Responsive styles
Murray has several ways to handle responsive styles.
Breakpoints
We work with the same breakpoints across all our apps:
xs | vs | sm | md | lg | xl | xxl |
|---|---|---|---|---|---|---|
| 0px | 420px | 640px | 800px | 960px | 1120px | 1440px |
Each breakpoint has 2 design tokens related to it: one for the min-width and one for the max-width. For example, the sm breakpoint has the following tokens:
ltSm, which coressponds tomax-width: 639px(theltstands for "less than")sm, which coressponds tomin-width: 640px
sx prop
For any component that supports the sx styling prop, it's easy to add responsive styles though media properties.
It can be used to change any style of a component:
Demo
It can also be used to hide or show a component. Please keep in mind that doing so will always keep both elements in the tree (they are present but hidden).
I'm displayed on smaller screens 📱
I'm displayed on larger screens 🖥️
Responsive styling through sx is the recommended approach in most cases as:
- it works universally (on both mobile and web)
- it is compatible with Server-Side-Rendering
- it is the most performant (it relies on CSS on web, and on inline styles and global listeners on mobile)
However, it's not compatible with legacy Murray components that don't support the sx prop.
useMedia hook
Sometimes sx is not enough for responsiveness. It can be either that:
- you need to change the styles of a component that doesn't support
sx - you need to completely unmount components dependening on the screen size
- you want to actually change logic (and not only styles)
For such situations, the useMedia hook can be used. It returns an object with media query tokens as keys and booleans as values:
I'm mounted on smaller screens 📱
This hook is optmized to avoid re-renders when the screen size changes without triggering a breakpoint change we're not listening to.
useResponsiveBreakpoints hook
Murray also provides a useResponsiveBreakpoints hook. This hook only works on web and can be replaced by the newer useMedia is most cases.
The only advantage of using this hook is that it can listen to the size of the parent <ResponsiveRoot /> container (instead of the page size)
I'm mounted on smaller screens 📱
cssUtils helpers
You might encouter usages of cssUtils's helpers: responsiveUp, responsiveDown, responsiveHideUp and responsiveHideDown.
These helpers can be used on legacy components that don't support the sx prop and rely on JSS or Emotion for styling.