1
mirror of https://github.com/jakejarvis/stitches-normalize.git synced 2025-04-26 18:58:27 -04:00

make system font stack and -webkit/-moz rules optional

This commit is contained in:
Jake Jarvis 2022-03-20 11:01:07 -04:00
parent 93cc857a35
commit 4be4db3a64
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
2 changed files with 141 additions and 33 deletions

View File

@ -1,7 +1,7 @@
# 🧵 stitches-normalize # 🧵 stitches-normalize
[![npm](https://img.shields.io/npm/v/stitches-normalize)](https://www.npmjs.com/package/stitches-normalize) [![npm](https://img.shields.io/npm/v/stitches-normalize)](https://www.npmjs.com/package/stitches-normalize)
[![MIT License](https://img.shields.io/github/license/jakejarvis/stitches-normalize?color=violet)](LICENSE) [![MIT License](https://img.shields.io/github/license/jakejarvis/stitches-normalize)](LICENSE)
@sindresorhus's [**modern-normalize.css**](https://github.com/sindresorhus/modern-normalize) as a plug-and-play JavaScript object compatible with Stitches. @sindresorhus's [**modern-normalize.css**](https://github.com/sindresorhus/modern-normalize) as a plug-and-play JavaScript object compatible with Stitches.
@ -23,10 +23,15 @@ You can read more about setting that file up [in Stitches' awesome documentation
import { globalCss } from "./stitches.config"; import { globalCss } from "./stitches.config";
import normalizeCss from "stitches-normalize"; import normalizeCss from "stitches-normalize";
const globalStyles = globalCss(...normalizeCss, { const globalStyles = globalCss(
...normalizeCss({
// default options, see below:
fontFamilies: true,
webkitPrefixes: true,
mozPrefixes: true,
}), {
// you can put the rest of your global styles here if necessary. // you can put the rest of your global styles here if necessary.
// these rules will override stitches-normalize's. // these rules will override stitches-normalize's.
body: { body: {
fontFamily: "'Comic Sans MS', sans-serif", fontFamily: "'Comic Sans MS', sans-serif",
}, },
@ -38,6 +43,35 @@ const App = () => {
}; };
``` ```
## API
### normalizeCss(options?)
#### options
Type: `object`
##### systemFonts
Type: `boolean`\
Default: `true`
Include the [default system font stacks](https://github.com/sindresorhus/modern-normalize/issues/3) (sans-serif fonts for `html`, monospace fonts for `code`, `kbd`, `samp`, and `pre`.)
##### webkitPrefixes
Type: `boolean`\
Default: `true`
Include non-standard WebKit compatibility rules for older Safari versions on iOS and macOS.
##### mozPrefixes
Type: `boolean`\
Default: `true`
Include non-standard Mozilla compatibility rules for older Firefox versions.
## License ## License
MIT MIT

View File

@ -3,20 +3,77 @@
import type * as Stitches from "@stitches/react"; import type * as Stitches from "@stitches/react";
const normalizeCss: Record<string, Stitches.CSSProperties>[] = [ export interface Options {
{ /**
* Include the [default system font stacks](https://github.com/sindresorhus/modern-normalize/issues/3) (sans-serif
* fonts for `html`, monospace fonts for `code`, `kbd`, `samp`, and `pre`.)
*
* @default true
*/
readonly systemFonts?: boolean;
/**
* Include non-standard WebKit compatibility rules for older Safari versions on iOS and macOS.
*
* @default true
*/
readonly webkitPrefixes?: boolean;
/**
* Include non-standard Mozilla compatibility rules for older Firefox versions.
*
* @default true
*/
readonly mozPrefixes?: boolean;
}
/**
* @returns A Stitches-compatible version of [modern-normalize.css](https://github.com/sindresorhus/modern-normalize).
*
* @example
* ```
* import { globalCss } from "./stitches.config";
* import normalizeCss from "stitches-normalize";
*
* const globalStyles = globalCss(
* ...normalizeCss({
* // default options, see below:
* fontFamilies: true,
* webkitPrefixes: true,
* mozPrefixes: true,
* }), {
* // you can put the rest of your global styles here if necessary.
* // these rules will override stitches-normalize's.
* body: {
* fontFamily: "'Comic Sans MS', sans-serif",
* },
* });
*
* const App = () => {
* globalStyles();
* return <h1>Hello, normalized world!</h1>;
* };
* ```
*/
const normalizeCss = (options: Options = {}): Record<string, Stitches.CSSProperties>[] => {
options = {
// all options default to true, which returns the complete modern-normalize.css equivalent
systemFonts: true,
webkitPrefixes: true,
mozPrefixes: true,
...options,
};
const baseStyles: Record<string, Stitches.CSSProperties> = {
"*, ::before, ::after": { "*, ::before, ::after": {
boxSizing: "border-box", boxSizing: "border-box",
}, },
html: { html: {
lineHeight: 1.15, lineHeight: 1.15,
tabSize: 4, tabSize: 4,
// @ts-ignore
WebkitTextSizeAdjust: "100%",
}, },
body: { body: {
margin: 0, margin: 0,
fontFamily: "system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'",
}, },
hr: { hr: {
height: 0, height: 0,
@ -29,7 +86,6 @@ const normalizeCss: Record<string, Stitches.CSSProperties>[] = [
fontWeight: "bolder", fontWeight: "bolder",
}, },
"code, kbd, samp, pre": { "code, kbd, samp, pre": {
fontFamily: "ui-monospace, SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace",
fontSize: "1em", fontSize: "1em",
}, },
small: { small: {
@ -60,20 +116,7 @@ const normalizeCss: Record<string, Stitches.CSSProperties>[] = [
"button, select": { "button, select": {
textTransform: "none", textTransform: "none",
}, },
"button, [type='button'], [type='reset'], [type='submit']": { "button, [type='button'], [type='reset'], [type='submit']": {},
// @ts-ignore
WebkitAppearance: "button",
},
"::-moz-focus-inner": {
borderStyle: "none",
padding: 0,
},
":-moz-focusring": {
outline: "1px dotted ButtonText",
},
":-moz-ui-invalid": {
boxShadow: "none",
},
legend: { legend: {
padding: 0, padding: 0,
}, },
@ -85,22 +128,53 @@ const normalizeCss: Record<string, Stitches.CSSProperties>[] = [
}, },
"[type='search']": { "[type='search']": {
outlineOffset: -2, outlineOffset: -2,
// @ts-ignore
WebkitAppearance: "textfield",
}, },
"::-webkit-search-decoration": { };
// append default system font stacks
if (options.systemFonts) {
baseStyles["body"].fontFamily = "system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'";
baseStyles["code, kbd, samp, pre"].fontFamily = "ui-monospace, SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace";
}
// `-webkit` compatibility properties and rules
if (options.webkitPrefixes) {
// @ts-ignore
baseStyles["html"].WebkitTextSizeAdjust = "100%";
// @ts-ignore
baseStyles["button, [type='button'], [type='reset'], [type='submit']"].WebkitAppearance = "button";
// @ts-ignore
baseStyles["[type='search']"].WebkitAppearance = "textfield";
baseStyles["::-webkit-search-decoration"] = {
// @ts-ignore // @ts-ignore
WebkitAppearance: "none", WebkitAppearance: "none",
}, };
"::-webkit-inner-spin-button, ::-webkit-outer-spin-button": { baseStyles["::-webkit-inner-spin-button, ::-webkit-outer-spin-button"] = {
height: "auto", height: "auto",
}, };
"::-webkit-file-upload-button": { baseStyles["::-webkit-file-upload-button"] = {
font: "inherit", font: "inherit",
// @ts-ignore // @ts-ignore
WebkitAppearance: "button", WebkitAppearance: "button",
}, };
}, }
];
// `-moz` compatibility properties and rules
if (options.mozPrefixes) {
baseStyles["::-moz-focus-inner"] = {
borderStyle: "none",
padding: 0,
};
baseStyles[":-moz-focusring"] = {
outline: "1px dotted ButtonText",
};
baseStyles[":-moz-ui-invalid"] = {
boxShadow: "none",
};
}
return [baseStyles];
};
export default normalizeCss; export default normalizeCss;