@spark-ui/theme-utils 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/createCSSTokensFile.mts +27 -1
- package/{createTailwindConfigFile.mts → createTailwindThemeConfigFile.mts} +12 -1
- package/createTheme.mts +12 -0
- package/index.mts +1 -1
- package/index.stories.mdx +219 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.1.1](https://github.com/adevinta/spark/compare/@spark-ui/theme-utils@1.1.0...@spark-ui/theme-utils@1.1.1) (2023-02-13)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @spark-ui/theme-utils
|
|
9
|
+
|
|
6
10
|
# [1.1.0](https://github.com/adevinta/spark/compare/@spark-ui/theme-utils@0.0.1...@spark-ui/theme-utils@1.1.0) (2023-02-10)
|
|
7
11
|
|
|
8
12
|
### Features
|
package/createCSSTokensFile.mts
CHANGED
|
@@ -2,6 +2,7 @@ import { appendFileSync, existsSync, mkdirSync } from 'fs'
|
|
|
2
2
|
import hexRgb from 'hex-rgb'
|
|
3
3
|
import parentModule from 'parent-module'
|
|
4
4
|
import { join } from 'path'
|
|
5
|
+
import type { RequireAtLeastOne } from 'type-fest'
|
|
5
6
|
|
|
6
7
|
import type { Theme } from './types.mjs'
|
|
7
8
|
import {
|
|
@@ -67,7 +68,32 @@ const getStringifiedThemes = (themeRecord: Record<string, Theme>) =>
|
|
|
67
68
|
: `.${className}{${toStringifiedTheme(rest)}}`
|
|
68
69
|
})
|
|
69
70
|
|
|
70
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Creates a CSS file containing theme tokens represented as CSS custom properties
|
|
73
|
+
*
|
|
74
|
+
* @param {string} path - The file path where the CSS file will be created.
|
|
75
|
+
* @param {Record<string, Theme>} themeRecord - A record (with a required key of "default") of themes that will be included in the CSS Tokens file.
|
|
76
|
+
*
|
|
77
|
+
* @returns {void}
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
*
|
|
81
|
+
* const defaultTheme: Theme = { ... }
|
|
82
|
+
* const darkTheme: Theme = { ... }
|
|
83
|
+
* const otherTheme: Theme = { ... }
|
|
84
|
+
*
|
|
85
|
+
* const themes = {
|
|
86
|
+
* default: defaultTheme,
|
|
87
|
+
* dark: darkTheme
|
|
88
|
+
* other: otherTheme
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* createCSSTokensFile('somePath.css', themes)
|
|
92
|
+
*/
|
|
93
|
+
export function createCSSTokensFile(
|
|
94
|
+
path: string,
|
|
95
|
+
themeRecord: RequireAtLeastOne<Record<string, Theme>, 'default'>
|
|
96
|
+
) {
|
|
71
97
|
const { filepath, rootPath } = buildFilePath(join(parentModule() || '', path))
|
|
72
98
|
|
|
73
99
|
const folders = filepath.split('/').slice(0, -1)
|
|
@@ -47,7 +47,18 @@ function toTailwindConfig(theme: Theme): TailwindConfig {
|
|
|
47
47
|
return toKebabCaseKeys(themeCpy)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Creates a Tailwind config file that links the [theme options](https://tailwindcss.com/docs/theme#configuration-reference) provided by Tailwind with the CSS custom property values generated using the "createCSSTokensFile" function
|
|
52
|
+
*
|
|
53
|
+
* @param {string} path - The file path where the Tailwind config file will be created.
|
|
54
|
+
*
|
|
55
|
+
* @returns {void}
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
*
|
|
59
|
+
* createTailwindThemeConfigFile('tailwind.theme.js')
|
|
60
|
+
*/
|
|
61
|
+
export function createTailwindThemeConfigFile(path: string) {
|
|
51
62
|
const { filepath, rootPath } = buildFilePath(join(parentModule() || '', path))
|
|
52
63
|
|
|
53
64
|
const folders = filepath.split('/').slice(0, -1)
|
package/createTheme.mts
CHANGED
|
@@ -4,6 +4,18 @@ import { PartialDeep } from 'type-fest'
|
|
|
4
4
|
import { defaultTheme } from './defaultTheme.mjs'
|
|
5
5
|
import type { Theme } from './types.mjs'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Create a custom theme by merging the default theme with a partial custom theme passed as an argument.
|
|
9
|
+
*
|
|
10
|
+
* @param {PartialDeep<Theme>} theme - A partial theme object of type PartialDeep<Theme> which holds the theme values that need to be customized or overridden
|
|
11
|
+
*
|
|
12
|
+
* @returns {Theme}
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* const alternativeTheme: PartialDeep<Theme> = { ... }
|
|
17
|
+
* const newTheme = createTheme(alternativeTheme)
|
|
18
|
+
*/
|
|
7
19
|
export function createTheme(theme: PartialDeep<Theme> = {}): Theme {
|
|
8
20
|
return deepMerge<Theme, PartialDeep<Theme>>(defaultTheme, theme)
|
|
9
21
|
}
|
package/index.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createTheme } from './createTheme.mjs'
|
|
2
|
-
export {
|
|
2
|
+
export { createTailwindThemeConfigFile } from './createTailwindThemeConfigFile.mjs'
|
|
3
3
|
export { createCSSTokensFile } from './createCSSTokensFile.mjs'
|
|
4
4
|
export { defaultTheme } from './defaultTheme.mjs'
|
|
5
5
|
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { Meta } from '@storybook/blocks'
|
|
2
|
+
import { StoryHeading } from '../../../documentation/helpers/StoryHeading/'
|
|
3
|
+
|
|
4
|
+
<Meta title="utils/Theme" />
|
|
5
|
+
|
|
6
|
+
# Theme
|
|
7
|
+
|
|
8
|
+
- [Theme](#theme-interface)
|
|
9
|
+
- [defaultTheme](#default-theme)
|
|
10
|
+
- [createTheme](#createtheme)
|
|
11
|
+
- [createCSSTokensFile](#createcsstokensfile)
|
|
12
|
+
- [createTailwindThemeConfigFile](#createtailwindthemeconfigfile)
|
|
13
|
+
- [Practical example](#practical-example)
|
|
14
|
+
|
|
15
|
+
> **Note**
|
|
16
|
+
>
|
|
17
|
+
> Spark Design System is built using the **Tailwind CSS** framework
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
The **`packages/utils/theme`** exposes the following:
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import {
|
|
25
|
+
Theme,
|
|
26
|
+
defaultTheme,
|
|
27
|
+
createTheme,
|
|
28
|
+
createCSSTokensFile,
|
|
29
|
+
createTailwindThemeConfigFile,
|
|
30
|
+
} from '@spark-ui/theme-utils'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<StoryHeading label="theme interface" as="h4" />
|
|
34
|
+
|
|
35
|
+
- `Theme`, the TypeScript interface for our theme
|
|
36
|
+
|
|
37
|
+
<StoryHeading label="default theme" as="h4" />
|
|
38
|
+
|
|
39
|
+
- `defaultTheme`, our default theme, represented as an `Object`
|
|
40
|
+
|
|
41
|
+
<StoryHeading label="createTheme" as="h4" />
|
|
42
|
+
|
|
43
|
+
- A function, that enables one to create a new theme by modifying specific values from the default theme without having to define a completely new theme from scratch.
|
|
44
|
+
|
|
45
|
+
This function expects one argument:
|
|
46
|
+
|
|
47
|
+
- A partial theme object which holds the theme values that need to be customized or overridden
|
|
48
|
+
|
|
49
|
+
**Usage:**
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
const someTheme: PartialDeep<Theme> = { ... }
|
|
53
|
+
const newTheme = createTheme(someTheme) // Theme
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
<StoryHeading label="createCSSTokensFile" as="h4" />
|
|
58
|
+
|
|
59
|
+
- A function, that will generate a `css` file containing all of the theme tokens, represented as CSS custom properties.
|
|
60
|
+
|
|
61
|
+
This function expects two arguments:
|
|
62
|
+
|
|
63
|
+
- The file path where the `css` file will be created
|
|
64
|
+
- A `Record` (with a required key of "default") of themes that will be included in the `css` file
|
|
65
|
+
|
|
66
|
+
**Usage:**
|
|
67
|
+
|
|
68
|
+
- Let's create a file (named `createTokens.ts` in our example) that will call this function
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
// createTokens.ts
|
|
72
|
+
const defaultTheme: Theme = { ... }
|
|
73
|
+
const darkTheme: Theme = { ... }
|
|
74
|
+
const someOtherTheme: Theme = { ... }
|
|
75
|
+
|
|
76
|
+
const themes = {
|
|
77
|
+
default: defaultTheme,
|
|
78
|
+
dark: darkTheme,
|
|
79
|
+
other: someOtherTheme
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
createCSSTokensFile('./somePath.css', themes)
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- Executing `createTokens.ts` will generate a `somePath.css` file with the following structure:
|
|
87
|
+
|
|
88
|
+
```css
|
|
89
|
+
@tailwind base;
|
|
90
|
+
@tailwind components;
|
|
91
|
+
@tailwind utilities;
|
|
92
|
+
|
|
93
|
+
@layer base {
|
|
94
|
+
:root {
|
|
95
|
+
/* :root, derived from the "default" key ↔️ themes.default */
|
|
96
|
+
--some-token: someValue;
|
|
97
|
+
--some-other-token: otherValue;
|
|
98
|
+
...;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.dark {
|
|
102
|
+
/* .dark, derived from the "dark" key ↔️ themes.dark */
|
|
103
|
+
--some-token: foo;
|
|
104
|
+
--some-other-token: bar;
|
|
105
|
+
...;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.other {
|
|
109
|
+
/* .other, derived from the "other" key ↔️ themes.other */
|
|
110
|
+
--some-token: lorem;
|
|
111
|
+
--some-other-token: ipsum;
|
|
112
|
+
...;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
<StoryHeading label="createTailwindThemeConfigFile" as="h4" />
|
|
118
|
+
|
|
119
|
+
A function that will generate a config file that links the [theme options](https://tailwindcss.com/docs/theme#configuration-reference) provided by Tailwind with the CSS custom property values generated using the `createCSSTokensFile` function.
|
|
120
|
+
|
|
121
|
+
This function expects one argument:
|
|
122
|
+
|
|
123
|
+
- The file path where the `css` file will be created
|
|
124
|
+
|
|
125
|
+
**Usage:**
|
|
126
|
+
|
|
127
|
+
- Let's create a file (named `overrideTailwindConfig.ts` in our example) that will call this function
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
// overrideTailwindConfig.ts
|
|
131
|
+
createTailwindThemeConfigFile('./tailwind.theme.js)
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- Executing `overrideTailwindConfig.ts` will generate a `tailwind.theme.js` file with the following structure:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
// tailwind.theme.js
|
|
139
|
+
module.exports = {
|
|
140
|
+
...,
|
|
141
|
+
screens: {
|
|
142
|
+
sm: 'var(--some-value)',
|
|
143
|
+
md: 'var(--some-other-value)',
|
|
144
|
+
},
|
|
145
|
+
spacing: {
|
|
146
|
+
sm: 'var(--some-foo)',
|
|
147
|
+
md: 'var(--some-bar)'
|
|
148
|
+
}
|
|
149
|
+
...
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
- Then, inside your Tailwind config file, include a reference to the file that will be generated upon executing `overrideTailwindConfig.ts`
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
// tailwind.config.js
|
|
157
|
+
const themeConf = require('./tailwind.theme.js') // ⬅️ generated by executing `overrideTailwindConfig.ts`
|
|
158
|
+
|
|
159
|
+
module.exports = {
|
|
160
|
+
theme: {
|
|
161
|
+
...themeConf, // ⬅️ replacing default Tailwind theme with our own
|
|
162
|
+
},
|
|
163
|
+
content: ['...'],
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
<StoryHeading label="Practical Example" as="h3" />
|
|
168
|
+
|
|
169
|
+
- For a more detailed understanding of how to employ these two functions together (**createCSSTokensFile** & **createTailwindThemeConfigFile**), here's a practical example of integrating them into your project:
|
|
170
|
+
|
|
171
|
+
1️⃣. Create a file (named `createThemeConfig.ts` in this example) that will call both of these functions
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
// createThemeConfig.ts
|
|
175
|
+
import { createCSSTokensFile, createTailwindThemeConfigFile } from '@spark-ui/theme-utils'
|
|
176
|
+
import { themes } from '/myThemes'
|
|
177
|
+
|
|
178
|
+
createTailwindThemeConfigFile('./tailwind.theme.js') // ⬅️ will generate the Tailwind theme config file
|
|
179
|
+
createCSSTokensFile('./tailwind.css', themes) // ⬅️ will generate the CSS tokens file
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
2️⃣ a. Inside the Tailwind config file, include a reference to the file that will be generated upon executing the file from step 1
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
// tailwind.config.js
|
|
186
|
+
const themeConf = require('./tailwind.theme.js') // ⬅️ generated by executing `createThemeConfig.ts`
|
|
187
|
+
|
|
188
|
+
module.exports = {
|
|
189
|
+
theme: {
|
|
190
|
+
...themeConf, // ⬅️ replacing default Tailwind theme with our own
|
|
191
|
+
},
|
|
192
|
+
content: ['...'],
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
2️⃣ b. Inside a file in your application where you import your global styles, include a reference to the file that will be generated upon executing the file from step 1
|
|
197
|
+
|
|
198
|
+
If you are using **Next.js**, that would be `_app.tsx`
|
|
199
|
+
|
|
200
|
+
```tsx
|
|
201
|
+
// some file in your application where you can import global styles
|
|
202
|
+
|
|
203
|
+
import '../tailwind.css' // ⬅️ generated by executing `createThemeConfig.ts`
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
3️⃣. Inside your `package.json`, add a script that will execute the file from step 1, and make sure to run that script before you start your application
|
|
207
|
+
|
|
208
|
+
```json
|
|
209
|
+
// package.json
|
|
210
|
+
{
|
|
211
|
+
...
|
|
212
|
+
"scripts": {
|
|
213
|
+
...,
|
|
214
|
+
"start": "npm run create:theme-config && start your-app",
|
|
215
|
+
"create:theme-config": "ts-node-esm createThemeConfig.ts", // ⬅️ executing `createThemeConfig.ts`
|
|
216
|
+
...
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spark-ui/theme-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Theme configuration",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,5 +17,5 @@
|
|
|
17
17
|
"parent-module": "3.0.0",
|
|
18
18
|
"type-fest": "3.5.6"
|
|
19
19
|
},
|
|
20
|
-
"gitHead": "
|
|
20
|
+
"gitHead": "88ef74d7ffbf238395c021350fb6e54fe50bba43"
|
|
21
21
|
}
|