Warning
stitches-mixins
is in maintenance modeThe package is stable and safe to use, but no new features will be added
Shorthand utils for Stitches 🥣
View the CodeSandbox Example 🪣
Stitches utils
are a great tool for reusing dynamic snippets of CSS across your project.
Unfortunately, for utils
that don't require a value
, shorthand isn't an option.
A common workaround is to set the util
value to true
:
// without stitches-mixins
const Button = styled("button", {
someUtilKey: true,
someOtherUtilKey: true,
color: "$gray900",
// …styles
});
stitches-mixins
offers an alternative; allowing snippets of static CSS to be included via the include
key:
// with stitches-mixins
const Button = styled("button", {
include: "someUtilKey",
// *or* include multiple…
include: ["someUtilKey", "someOtherUtilKey"],
});
To kickstart your mixins toolbox, stitches-mixins
includes the following by default:
Key | Description |
---|---|
box |
Layout primitive [1] |
breakout |
"Breakout" of a parent's maxWidth to fill the viewport width [2] |
minHeightScreen |
Fills the viewport height, with additional support for iOS Safari. |
screenReaderOnly |
Hides an element visually without hiding from screen readers and other ATs [3] |
notScreenReaderOnly |
Reverts styles set by screenReaderOnly [3] |
Default mixins can be overridden by defining custom mixins with the same key
.
-
Install the package via your favourite package manager:
npm i stitches-mixins
-
In your Stitches config, assign
mixins()
to a newinclude
util
:// stitches.config.ts import { createStitches } from "@stitches/react"; import { mixins } from "stitches-mixins"; export const { css, styled } = createStitches({ theme: {}, utils: { // with custom mixins include: mixins({ orchidShadow: { boxShadow: "0 25px 50px -12px orchid", }, }), // …or without include: mixins(), }, });
Note: Your
stitches-mixins
util doesn't need to be calledinclude
, it can be anything you want it to be.
Use include
like you would with any other Stitches util
.
View the CodeSandbox Demo 🪣
💡 Using
include
at the beginning of your style object is heavily recommended, allowing for easy overriding later
// components/card.ts
import { styled } from "../stitches.config";
const Card = styled("div", {
include: "box",
// ...styles
});
// components/card.ts
import { styled } from "../stitches.config";
const Card = styled("div", {
include: ["box", "orchidShadow"],
// ...styles
});
Like other utils
, mixins can be used inside other selectors:
// components/skip-link.ts
import { styled } from "../stitches.config";
const SkipLink = styled("a", {
include: ["box", "screenReaderOnly"],
"&:focus": {
include: "notScreenReaderOnly",
},
// ...styles
});
- ^ Brent Jackson. Reflexbox.
Styles used with additional pseudo styles, and without margin reset. - ^ Sven Wolfermann. "full viewport width image (container) inside article" (Codepen).
Styles used without modification. - ^ Tailwind. Utilities for improving accessibility with screen readers.
Styles used without modification.