stitches-mixins v1.0.0
Table of Contents
Introduction
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
});Solution
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"],
});Default Mixins
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] |
Overriding
Default mixins can be overridden by defining custom mixins with the same key.
Setup
Install the package via your favourite package manager:
npm i stitches-mixinsIn your Stitches config, assign
mixins()to a newincludeutil:// stitches.config.ts import { createCss } from "@stitches/react"; import { mixins } from "stitches-mixins"; export const { css, styled } = createCss({ theme: {}, utils: { // with custom mixins include: mixins({ orchidShadow: { boxShadow: "0 25px 50px -12px orchid", }, }), // …or without include: mixins(), }, });Note: Your
stitches-mixinsutil doesn't need to be calledinclude, it can be anything you want it to be.
Usage
Use include like you would with any other Stitches util.
💡 Using
includeat the beginning of your style object is heavily recommended, allowing for easy overriding later
Single-use
// components/card.ts
import { styled } from "../stitches.config";
const Card = styled("div", {
include: "box",
// ...styles
});Combining Mixins
// components/card.ts
import { styled } from "../stitches.config";
const Card = styled("div", {
include: ["box", "orchidShadow"],
// ...styles
});Nested
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
});Credit
- ^ 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.
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago