makeStaticStyles
Creates styles with a global selector. This is especially useful for CSS resets, for example normalize.css.
makeStaticStyles returns a React hook that should be called inside a component.
Defining styles with objectsโ
import { makeStaticStyles } from '@griffel/react';
const useStaticStyles = makeStaticStyles({
'@font-face': {
fontFamily: 'Open Sans',
src: `url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),
url("/fonts/OpenSans-Regular-webfont.woff") format("woff")`,
},
body: {
backgroundColor: 'red',
},
});
function App() {
useStaticStyles();
return <div />;
}
Produces following CSS...
@font-face {
font-family: 'Open Sans';
src: url('/fonts/OpenSans-Regular-webfont.woff2') format('woff2'), url('/fonts/OpenSans-Regular-webfont.woff') format('woff');
}
body {
background-color: red;
}
note
Be sure the correctly quote font family names with things like parentheses in them:
// โ
Correctly returns `font-family` in styles
const useStaticStyles = makeStaticStyles({
'@font-face': {
fontFamily: '"Segoe UI Web (West European)"',
// ...
},
});
// ๐ด Will not return a `font-family` in styles
const useStaticStyles = makeStaticStyles({
'@font-face': {
fontFamily: 'Segoe UI Web (West European)',
// ...
},
});
Limited support for nested selectors
Nested selectors are not supported for this scenario via nesting of JavaScript objects.
import { makeStaticStyles } from '@griffel/react';
const useStaticStyles = makeStaticStyles({
// ๐ด Not supported
'.some': {
'.class': {
/* ... */
},
':hover': {
/* ... */
},
},
// โ
Supported
'.some.class': {
/* ... */
},
'.some.class:hover': {
/* ... */
},
});