├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .nvmrc ├── LICENSE ├── README.md ├── example ├── index.html ├── package.json ├── src │ ├── App.tsx │ ├── main.tsx │ ├── stitches.config.ts │ └── vite-env.d.ts ├── tsconfig.json └── vite.config.ts ├── package.json ├── package ├── package-lock.json ├── package.json ├── src │ ├── index.ts │ └── private.ts └── tsconfig.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml └── test ├── .babelrc ├── __snapshots__ └── index.test.ts.snap ├── index.test.ts ├── package.json └── tsconfig.json /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | os: 16 | - ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Read .nvmrc 21 | run: echo ::set-output name=NVMRC::$(cat .nvmrc) 22 | id: nvm 23 | 24 | - name: Setup Node.js 25 | uses: actions/setup-node@v2.4.0 26 | with: 27 | node-version: "${{ steps.nvm.outputs.NVMRC }}" 28 | 29 | - run: npm run setup && pnpm install 30 | - run: pnpm run build 31 | - run: npx prettier . --check --ignore-path .gitignore 32 | - run: pnpm run lint 33 | env: 34 | CI: true 35 | - run: pnpm run test 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | .husky -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 14.17.6 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > **Warning** 2 | > 3 | > **`stitches-mixins` is in maintenance mode** 4 | > 5 | > The package is stable and safe to use, but no new features will be added 6 | 7 | --- 8 | 9 |

Stitches Mixins

10 | 11 |

12 | 13 | Shorthand utils for Stitches 🥣 14 | 15 |

16 | 17 |

18 | View the CodeSandbox Example 🪣 19 |

20 | 21 |

22 | 23 | NPM Version 24 | 25 | 26 | Types Included 27 | 28 | 29 | Apache-2.0 License 30 | 31 | 32 | NPM Downloads 33 | 34 | 35 | Follow @joebell_ on Twitter 36 | 37 |

38 | 39 |
40 | 41 | ## Table of Contents 42 | 43 | 1. [Introduction](#introduction) 44 | 1. [Default Mixins](#default-mixins) 45 | 1. [Setup](#setup) 46 | 1. [Usage](#usage) 47 | 1. [Credit](#credit) 48 | 49 | ## Introduction 50 | 51 | [Stitches `utils`][stitches:utils] are a great tool for reusing dynamic snippets of CSS across your project. 52 | 53 | Unfortunately, for `utils` that **don't** require a `value`, [shorthand isn't an option][mdn:initializer]. 54 | 55 | A common workaround is to set the `util` value to `true`: 56 | 57 | ```ts 58 | // without stitches-mixins 59 | const Button = styled("button", { 60 | someUtilKey: true, 61 | someOtherUtilKey: true, 62 | color: "$gray900", 63 | // …styles 64 | }); 65 | ``` 66 | 67 | ### Solution 68 | 69 | `stitches-mixins` offers an alternative; allowing snippets of static CSS to be included via the `include` key: 70 | 71 | ```ts 72 | // with stitches-mixins 73 | const Button = styled("button", { 74 | include: "someUtilKey", 75 | // *or* include multiple… 76 | include: ["someUtilKey", "someOtherUtilKey"], 77 | }); 78 | ``` 79 | 80 | ## Default Mixins 81 | 82 | To kickstart your mixins toolbox, `stitches-mixins` includes the following by default: 83 | 84 | | Key | Description | 85 | | --------------------- | ----------------------------------------------------------------------------------------------------------------------------- | 86 | | `box` | Layout primitive [[1]](#credit_ref-1) | 87 | | `breakout` | "Breakout" of a parent's `maxWidth` to fill the viewport width [[2]](#credit_ref-2) | 88 | | `minHeightScreen` | Fills the viewport height, with additional support for iOS Safari. | 89 | | `screenReaderOnly` | Hides an element visually without hiding from screen readers and other ATs [[3]](#credit_ref-3) | 90 | | `notScreenReaderOnly` | Reverts styles set by `screenReaderOnly` [[3]](#credit_ref-3) | 91 | 92 | ### Overriding 93 | 94 | Default mixins can be overridden by defining custom mixins with the same `key`. 95 | 96 | ## Setup 97 | 98 | 1. Install the package via your favourite package manager: 99 | 100 | ```sh 101 | npm i stitches-mixins 102 | ``` 103 | 104 | 2. In your [Stitches config][stitches:config], assign `mixins()` to a new `include` `util`: 105 | 106 | ```tsx 107 | // stitches.config.ts 108 | import { createStitches } from "@stitches/react"; 109 | import { mixins } from "stitches-mixins"; 110 | 111 | export const { css, styled } = createStitches({ 112 | theme: {}, 113 | utils: { 114 | // with custom mixins 115 | include: mixins({ 116 | orchidShadow: { 117 | boxShadow: "0 25px 50px -12px orchid", 118 | }, 119 | }), 120 | // …or without 121 | include: mixins(), 122 | }, 123 | }); 124 | ``` 125 | 126 | > Note: Your `stitches-mixins` util doesn't _need_ to be called `include`, it can be anything you want it to be. 127 | 128 | ## Usage 129 | 130 | Use `include` like you would with any other [Stitches `util`][stitches:utils]. 131 | 132 | [View the CodeSandbox Demo 🪣][demo] 133 | 134 | > 💡 Using `include` at the beginning of your style object is heavily recommended, allowing for easy overriding later 135 | 136 | ### Single-use 137 | 138 | ```tsx 139 | // components/card.ts 140 | import { styled } from "../stitches.config"; 141 | 142 | const Card = styled("div", { 143 | include: "box", 144 | // ...styles 145 | }); 146 | ``` 147 | 148 | ### Combining Mixins 149 | 150 | ```tsx 151 | // components/card.ts 152 | import { styled } from "../stitches.config"; 153 | 154 | const Card = styled("div", { 155 | include: ["box", "orchidShadow"], 156 | // ...styles 157 | }); 158 | ``` 159 | 160 | ### Nested 161 | 162 | Like other `utils`, mixins can be used inside other selectors: 163 | 164 | ```tsx 165 | // components/skip-link.ts 166 | import { styled } from "../stitches.config"; 167 | 168 | const SkipLink = styled("a", { 169 | include: ["box", "screenReaderOnly"], 170 | "&:focus": { 171 | include: "notScreenReaderOnly", 172 | }, 173 | // ...styles 174 | }); 175 | ``` 176 | 177 | ## Credit 178 | 179 | 1. [**^**](#credit_note-1) Brent Jackson. [Reflexbox][credit:box]. 180 | _Styles used with additional pseudo styles, and without margin reset._ 181 | 2. [**^**](#credit_note-2) Sven Wolfermann. ["full viewport width image (container) inside article" (Codepen)][credit:breakout]. 182 | _Styles used without modification._ 183 | 3. [**^**](#credit_note-3) Tailwind. [Utilities for improving accessibility with screen readers][credit:screenreaderonly]. 184 | _Styles used without modification._ 185 | 186 | [demo]: https://codesandbox.io/s/github/joe-bell/stitches-mixins/tree/main/example?file=/src/App.tsx 187 | [credit:box]: https://github.com/rebassjs/rebass/tree/master/packages/reflexbox 188 | [credit:breakout]: https://codepen.io/maddesigns/pen/rOMgpQ/ 189 | [credit:screenreaderonly]: https://tailwindcss.com/docs/screen-readers 190 | [mdn:initializer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer 191 | [stitches]: https://github.com/modulz/stitches 192 | [stitches:config]: https://stitches.dev/docs/installation#create-your-config-file 193 | [stitches:utils]: https://stitches.dev/docs/utils 194 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Stitches Mixins 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "example", 4 | "scripts": { 5 | "lint": "tsc", 6 | "start": "vite", 7 | "build": "vite build" 8 | }, 9 | "dependencies": { 10 | "@stitches/react": "1.1.0", 11 | "stitches-mixins": "latest", 12 | "react": "^17.0.0", 13 | "react-dom": "^17.0.0" 14 | }, 15 | "devDependencies": { 16 | "@types/react": "17.0.0", 17 | "@types/react-dom": "17.0.0", 18 | "@vitejs/plugin-react-refresh": "1.3.1", 19 | "npe": "1.1.4", 20 | "typescript": "4.4.3", 21 | "vite": "2.5.1" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/src/App.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { styled, globalCss } from "./stitches.config"; 3 | 4 | const globalStyles = globalCss({ 5 | body: { 6 | margin: 0, 7 | }, 8 | }); 9 | 10 | const Box = styled("div", { 11 | include: "box", 12 | }); 13 | 14 | const Card = styled("div", { 15 | include: ["box", "orchidShadow"], 16 | padding: "$4", 17 | borderRadius: "$space$2", 18 | }); 19 | 20 | const Text = styled("p", { 21 | include: "box", 22 | fontFamily: "$system", 23 | }); 24 | 25 | const Link = styled("a", { 26 | include: "box", 27 | fontFamily: "$system", 28 | textDecoration: "none", 29 | color: "$purple600", 30 | }); 31 | 32 | const Container = styled("div", { 33 | include: "box", 34 | marginX: "auto", 35 | paddingX: "$3", 36 | variants: { 37 | size: { 38 | "1": { 39 | maxWidth: "400px", 40 | }, 41 | "2": { 42 | maxWidth: "585px", 43 | }, 44 | "3": { 45 | maxWidth: "865px", 46 | }, 47 | }, 48 | }, 49 | }); 50 | 51 | export default function App() { 52 | globalStyles(); 53 | return ( 54 | 61 | 62 | 63 | Hello, from Stitches Mixins. 64 | 65 | For full documentation, visit{" "} 66 | 67 | GitHub 68 | 69 | 70 | 71 | 72 | 73 | ); 74 | } 75 | -------------------------------------------------------------------------------- /example/src/main.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | 5 | ReactDOM.render( 6 | 7 | 8 | , 9 | document.getElementById("root") 10 | ); 11 | -------------------------------------------------------------------------------- /example/src/stitches.config.ts: -------------------------------------------------------------------------------- 1 | import type * as Stitches from "@stitches/react"; 2 | import { createStitches } from "@stitches/react"; 3 | import { mixins } from "stitches-mixins"; 4 | 5 | export const { styled, css, globalCss } = createStitches({ 6 | theme: { 7 | colors: { 8 | hiContrast: "hsl(206,10%,5%)", 9 | loContrast: "white", 10 | 11 | orchid: "orchid", 12 | 13 | gray100: "hsl(206,22%,99%)", 14 | gray200: "hsl(206,12%,97%)", 15 | gray300: "hsl(206,11%,92%)", 16 | gray400: "hsl(206,10%,84%)", 17 | gray500: "hsl(206,10%,76%)", 18 | gray600: "hsl(206,10%,44%)", 19 | 20 | purple100: "hsl(252,100%,99%)", 21 | purple200: "hsl(252,100%,98%)", 22 | purple300: "hsl(252,100%,94%)", 23 | purple400: "hsl(252,75%,84%)", 24 | purple500: "hsl(252,78%,60%)", 25 | purple600: "hsl(252,80%,53%)", 26 | }, 27 | space: { 28 | 1: "5px", 29 | 2: "10px", 30 | 3: "15px", 31 | 4: "20px", 32 | 5: "25px", 33 | 6: "35px", 34 | }, 35 | sizes: { 36 | 1: "5px", 37 | 2: "10px", 38 | 3: "15px", 39 | 4: "20px", 40 | 5: "25px", 41 | 6: "35px", 42 | }, 43 | fontSizes: { 44 | 1: "12px", 45 | 2: "13px", 46 | 3: "15px", 47 | 4: "17px", 48 | 5: "19px", 49 | 6: "21px", 50 | }, 51 | fonts: { 52 | system: "system-ui", 53 | }, 54 | }, 55 | media: { 56 | sm: "(min-width: 520px)", 57 | md: "(min-width: 900px)", 58 | }, 59 | utils: { 60 | include: mixins({ 61 | orchidShadow: { boxShadow: "0 25px 50px -12px $colors$orchid" }, 62 | }), 63 | 64 | marginX: (value: Stitches.PropertyValue<"marginLeft">) => ({ 65 | marginLeft: value, 66 | marginRight: value, 67 | }), 68 | marginY: (value: Stitches.PropertyValue<"marginTop">) => ({ 69 | marginTop: value, 70 | marginBottom: value, 71 | }), 72 | paddingX: (value: Stitches.PropertyValue<"paddingLeft">) => ({ 73 | paddingLeft: value, 74 | paddingRight: value, 75 | }), 76 | paddingY: (value: Stitches.PropertyValue<"paddingTop">) => ({ 77 | paddingTop: value, 78 | paddingBottom: value, 79 | }), 80 | }, 81 | }); 82 | -------------------------------------------------------------------------------- /example/src/vite-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "lib": ["DOM", "DOM.Iterable", "ESNext"], 5 | "allowJs": false, 6 | "skipLibCheck": true, 7 | "esModuleInterop": false, 8 | "allowSyntheticDefaultImports": true, 9 | "strict": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "module": "ESNext", 12 | "moduleResolution": "Node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "noEmit": true, 16 | "jsx": "react" 17 | }, 18 | "include": ["./src"] 19 | } 20 | -------------------------------------------------------------------------------- /example/vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "vite"; 2 | import reactRefresh from "@vitejs/plugin-react-refresh"; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [reactRefresh()], 7 | }); 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "root", 4 | "scripts": { 5 | "preinstall": "npx only-allow pnpm", 6 | "build": "pnpm run build --filter ./package && pnpm run build --filter ./example", 7 | "lint": "syncpack list-mismatches && pnpm run lint --r --parallel", 8 | "test": "pnpm run test --r --stream", 9 | "setup": "npm i -g pnpm@6.15.0", 10 | "start": "pnpm run start --filter example", 11 | "commit": "git-cz" 12 | }, 13 | "engines": { 14 | "pnpm": ">=6.14.3" 15 | }, 16 | "config": { 17 | "commitizen": { 18 | "path": "cz-conventional-changelog" 19 | } 20 | }, 21 | "commitlint": { 22 | "extends": [ 23 | "@commitlint/config-conventional" 24 | ] 25 | }, 26 | "husky": { 27 | "hooks": { 28 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", 29 | "pre-commit": "pnpm run lint && lint-staged" 30 | } 31 | }, 32 | "lint-staged": { 33 | "*": "prettier --ignore-unknown --write" 34 | }, 35 | "syncpack": { 36 | "peer": false, 37 | "versionGroups": [ 38 | { 39 | "dependencies": [ 40 | "stitches-mixins" 41 | ], 42 | "packages": [ 43 | "example" 44 | ] 45 | } 46 | ] 47 | }, 48 | "devDependencies": { 49 | "@commitlint/cli": "13.1.0", 50 | "@commitlint/config-conventional": "13.1.0", 51 | "commitizen": "4.2.4", 52 | "cz-conventional-changelog": "3.3.0", 53 | "husky": "4.3.8", 54 | "lint-staged": "11.1.2", 55 | "prettier": "2.4.0", 56 | "syncpack": "5.8.15" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /package/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stitches-mixins", 3 | "version": "1.0.0", 4 | "description": "Shorthand utils for Stitches 🥣", 5 | "source": "src/index.ts", 6 | "main": "dist/index.js", 7 | "module": "dist/index.esm.js", 8 | "typings": "dist/index.d.ts", 9 | "files": [ 10 | "dist", 11 | "README.md" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/joe-bell/stitches-mixins.git" 16 | }, 17 | "author": "Joe Bell (https://joebell.co.uk)", 18 | "license": "Apache-2.0", 19 | "bugs": { 20 | "url": "https://github.com/joe-bell/stitches-mixins/issues" 21 | }, 22 | "homepage": "https://github.com/joe-bell/stitches-mixins#readme", 23 | "jest": { 24 | "testMatch": [ 25 | "**/test/*.{ts,tsx}" 26 | ] 27 | }, 28 | "scripts": { 29 | "prepublishOnly": "npm run build", 30 | "build": "microbundle --no-compress --tsconfig tsconfig.json", 31 | "start": "npm run build -- watch", 32 | "clean": "rimraf {dist,.rts2*}", 33 | "lint": "tsc" 34 | }, 35 | "peerDependencies": { 36 | "@stitches/core": ">= 1.1.0", 37 | "@stitches/react": ">= 1.1.0" 38 | }, 39 | "peerDependenciesMeta": { 40 | "@stitches/core": { 41 | "optional": true 42 | }, 43 | "@stitches/react": { 44 | "optional": true 45 | } 46 | }, 47 | "devDependencies": { 48 | "@stitches/core": "1.1.0", 49 | "lint-staged": "11.1.2", 50 | "microbundle": "0.13.3", 51 | "rimraf": "3.0.2", 52 | "typescript": "4.4.3" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /package/src/index.ts: -------------------------------------------------------------------------------- 1 | import type { CSS } from "@stitches/core"; 2 | import { defaultMixins, TDefaultMixin } from "./private"; 3 | 4 | export interface IMixins { 5 | [key: string]: CSS; 6 | } 7 | 8 | export const mixins = 9 | (customMixins?: T) => 10 | (key: keyof T | TDefaultMixin | (keyof T | TDefaultMixin)[]) => { 11 | const mixins = { ...defaultMixins, ...customMixins }; 12 | 13 | return Array.isArray(key) 14 | ? key.reduce((acc, cv) => ({ ...acc, ...mixins[cv] }), {}) 15 | : mixins[key]; 16 | }; 17 | -------------------------------------------------------------------------------- /package/src/private.ts: -------------------------------------------------------------------------------- 1 | import type { CSS } from "@stitches/core"; 2 | 3 | export type TDefaultMixin = 4 | | "box" 5 | | "breakout" 6 | | "minHeightScreen" 7 | | "screenReaderOnly" 8 | | "notScreenReaderOnly"; 9 | 10 | export interface IDefaultMixins extends Record {} 11 | 12 | export const defaultMixins: IDefaultMixins = { 13 | box: { 14 | minWidth: 0, 15 | boxSizing: "border-box", 16 | "&::before, &::after": { 17 | boxSizing: "inherit", 18 | }, 19 | }, 20 | breakout: { 21 | width: "100vw", 22 | position: "relative", 23 | left: "50%", 24 | right: "50%", 25 | marginLeft: "-50vw", 26 | marginRight: "-50vw", 27 | }, 28 | minHeightScreen: { 29 | minHeight: "100vh", 30 | "@supports (-webkit-touch-callout: none)": { 31 | minHeight: "-webkit-fill-available", 32 | }, 33 | }, 34 | screenReaderOnly: { 35 | position: "absolute", 36 | width: "1px", 37 | height: "1px", 38 | padding: "0", 39 | margin: "-1px", 40 | overflow: "hidden", 41 | clip: "rect(0, 0, 0, 0)", 42 | whiteSpace: "nowrap", 43 | borderWidth: "0", 44 | }, 45 | notScreenReaderOnly: { 46 | position: "static", 47 | width: "auto", 48 | height: "auto", 49 | padding: "0", 50 | margin: "0", 51 | overflow: "visible", 52 | clip: "auto", 53 | whiteSpace: "normal", 54 | }, 55 | }; 56 | -------------------------------------------------------------------------------- /package/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "exclude": ["node_modules"], 3 | "include": ["src/**/*.ts"], 4 | "compilerOptions": { 5 | "target": "es5", 6 | "lib": ["dom", "dom.iterable", "esnext"], 7 | "allowJs": true, 8 | "skipLibCheck": true, 9 | "strict": false, 10 | "forceConsistentCasingInFileNames": true, 11 | "noEmit": true, 12 | "esModuleInterop": true, 13 | "module": "esnext", 14 | "moduleResolution": "node", 15 | "resolveJsonModule": true, 16 | "isolatedModules": false, 17 | "jsx": "preserve" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - "example" 3 | - "package" 4 | - "test" 5 | -------------------------------------------------------------------------------- /test/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "test": { 4 | "presets": [ 5 | ["@babel/preset-env", { "targets": { "node": "current" } }], 6 | "@babel/preset-typescript" 7 | ] 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/__snapshots__/index.test.ts.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`mixins custom mixins can be included 1`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}"`; 4 | 5 | exports[`mixins custom mixins can be included 2`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}--sxs{--sxs:2 c-jNRlAV}@media{.c-jNRlAV{min-width:0;box-sizing:border-box}.c-jNRlAV::before,.c-jNRlAV::after{box-sizing:inherit}}"`; 6 | 7 | exports[`mixins individual mixins can be included and overridden 1`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}"`; 8 | 9 | exports[`mixins individual mixins can be included and overridden 2`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}--sxs{--sxs:2 c-fovnzX}@media{.c-fovnzX{min-width:0;box-sizing:border-box}.c-fovnzX::before,.c-fovnzX::after{box-sizing:inherit}.c-fovnzX{margin:var(--space-md)}}"`; 10 | 11 | exports[`mixins mixins can be nested 1`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}"`; 12 | 13 | exports[`mixins mixins can be nested 2`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}--sxs{--sxs:2 c-jqIsIq}@media{.c-jqIsIq{min-width:0;box-sizing:border-box}.c-jqIsIq::before,.c-jqIsIq::after{box-sizing:inherit}.c-jqIsIq{box-shadow:0 25px 50px -12px var(--shadows-orchid);position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}.c-jqIsIq:focus{position:static;width:auto;height:auto;padding:0;margin:0;overflow:visible;clip:auto;white-space:normal}}"`; 14 | 15 | exports[`mixins multiple mixins can be included and overridden 1`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}"`; 16 | 17 | exports[`mixins multiple mixins can be included and overridden 2`] = `"--sxs{--sxs:0 t-gCfUPt}@media{:root,.t-gCfUPt{--colors-orchid:orchid;--space-md:1rem}}--sxs{--sxs:2 c-bIRQaM}@media{.c-bIRQaM{min-width:0;box-sizing:border-box}.c-bIRQaM::before,.c-bIRQaM::after{box-sizing:inherit}.c-bIRQaM{min-height:100vh}@supports (-webkit-touch-callout: none){.c-bIRQaM{min-height:-webkit-fill-available}}.c-bIRQaM{margin:var(--space-md)}}"`; 18 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import { createStitches } from "@stitches/core"; 2 | import { mixins } from "stitches-mixins"; 3 | 4 | const theme = { colors: { orchid: "orchid" }, space: { md: "1rem" } }; 5 | 6 | const customMixins = { 7 | orchidShadow: { 8 | boxShadow: "0 25px 50px -12px $orchid", 9 | }, 10 | }; 11 | 12 | describe("mixins", () => { 13 | test("individual mixins can be included and overridden", () => { 14 | const stitches = createStitches({ 15 | theme, 16 | utils: { 17 | include: mixins(), 18 | }, 19 | }); 20 | 21 | const component = stitches.css({ 22 | include: "box", 23 | margin: "$md", 24 | }); 25 | 26 | // CSS should contain theme etc… 27 | expect(stitches.toString()).toMatchSnapshot(); 28 | // Create component CSS 29 | component.toString(); 30 | // CSS should now contain component CSS 31 | expect(stitches.toString()).toMatchSnapshot(); 32 | }); 33 | 34 | test("multiple mixins can be included and overridden", () => { 35 | const stitches = createStitches({ 36 | theme, 37 | utils: { 38 | include: mixins(), 39 | }, 40 | }); 41 | 42 | const component = stitches.css({ 43 | include: ["box", "minHeightScreen"], 44 | margin: "$md", 45 | }); 46 | 47 | // CSS should contain theme etc… 48 | expect(stitches.toString()).toMatchSnapshot(); 49 | // Create component CSS 50 | component.toString(); 51 | // CSS should now contain component CSS 52 | expect(stitches.toString()).toMatchSnapshot(); 53 | }); 54 | 55 | test("custom mixins can be included", () => { 56 | const stitches = createStitches({ 57 | theme, 58 | utils: { 59 | include: mixins(customMixins), 60 | }, 61 | }); 62 | 63 | const component = stitches.css({ 64 | include: ["box", "orchidShadow"], 65 | }); 66 | 67 | // CSS should contain theme etc… 68 | expect(stitches.toString()).toMatchSnapshot(); 69 | // Create component CSS 70 | component.toString(); 71 | // CSS should now contain component CSS 72 | expect(stitches.toString()).toMatchSnapshot(); 73 | }); 74 | 75 | test("mixins can be nested", () => { 76 | const stitches = createStitches({ 77 | theme, 78 | utils: { 79 | include: mixins(customMixins), 80 | someShorthandUtil: (value: boolean) => 81 | value && { 82 | boxShadow: "0 25px 50px -12px orchid", 83 | }, 84 | }, 85 | }); 86 | 87 | const component = stitches.css({ 88 | include: ["box", "orchidShadow", "screenReaderOnly"], 89 | "&:focus": { 90 | include: "notScreenReaderOnly", 91 | }, 92 | }); 93 | 94 | // CSS should contain theme etc… 95 | expect(stitches.toString()).toMatchSnapshot(); 96 | // Create component CSS 97 | component.toString(); 98 | // CSS should now contain component CSS 99 | expect(stitches.toString()).toMatchSnapshot(); 100 | }); 101 | }); 102 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "test", 4 | "files": [ 5 | "dist", 6 | "README.md" 7 | ], 8 | "jest": { 9 | "testMatch": [ 10 | "**/test/*.{ts,tsx}" 11 | ] 12 | }, 13 | "scripts": { 14 | "test": "jest", 15 | "lint": "tsc" 16 | }, 17 | "dependencies": { 18 | "@stitches/core": "1.1.0", 19 | "stitches-mixins": "workspace:*" 20 | }, 21 | "devDependencies": { 22 | "@babel/core": "7.15.5", 23 | "@babel/preset-env": "7.15.6", 24 | "@babel/preset-typescript": "7.15.0", 25 | "@stitches/core": "1.1.0", 26 | "@types/jest": "27.0.1", 27 | "babel-jest": "27.1.1", 28 | "jest": "27.1.1", 29 | "typescript": "4.4.3" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "exclude": ["node_modules"], 3 | "include": ["**/*.test.ts"], 4 | "compilerOptions": { 5 | "target": "es5", 6 | "lib": ["dom", "dom.iterable", "esnext"], 7 | "allowJs": true, 8 | "skipLibCheck": true, 9 | "strict": false, 10 | "forceConsistentCasingInFileNames": true, 11 | "noEmit": true, 12 | "esModuleInterop": true, 13 | "module": "esnext", 14 | "moduleResolution": "node", 15 | "resolveJsonModule": true, 16 | "isolatedModules": false, 17 | "jsx": "preserve" 18 | } 19 | } 20 | --------------------------------------------------------------------------------