@spark-ui/theme-utils 1.1.1 → 2.0.0

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 CHANGED
@@ -3,6 +3,22 @@
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
+ # [2.0.0](https://github.com/adevinta/spark/compare/@spark-ui/theme-utils@1.2.0...@spark-ui/theme-utils@2.0.0) (2023-02-14)
7
+
8
+ ### Features
9
+
10
+ - split the theme utils package into two distinct packages ([77def01](https://github.com/adevinta/spark/commit/77def01abd8b2ccf4f234093133294071e407231)), closes [#235](https://github.com/adevinta/spark/issues/235)
11
+
12
+ ### BREAKING CHANGES
13
+
14
+ - Split the theme utils package into two distinct packages
15
+
16
+ # [1.2.0](https://github.com/adevinta/spark/compare/@spark-ui/theme-utils@1.1.1...@spark-ui/theme-utils@1.2.0) (2023-02-13)
17
+
18
+ ### Features
19
+
20
+ - **theme-utils:** improve build process for the theme-utils package ([7e53f89](https://github.com/adevinta/spark/commit/7e53f89bb80c7da4f5816b3bdb64b5b9a23d4874)), closes [#219](https://github.com/adevinta/spark/issues/219)
21
+
6
22
  ## [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
23
 
8
24
  **Note:** Version bump only for package @spark-ui/theme-utils
@@ -1,9 +1,5 @@
1
- import deepMerge from 'deepmerge'
2
- import { PartialDeep } from 'type-fest'
3
-
4
- import { defaultTheme } from './defaultTheme.mjs'
5
- import type { Theme } from './types.mjs'
6
-
1
+ import { PartialDeep } from 'type-fest';
2
+ import type { Theme } from './types';
7
3
  /**
8
4
  * Create a custom theme by merging the default theme with a partial custom theme passed as an argument.
9
5
  *
@@ -16,6 +12,4 @@ import type { Theme } from './types.mjs'
16
12
  * const alternativeTheme: PartialDeep<Theme> = { ... }
17
13
  * const newTheme = createTheme(alternativeTheme)
18
14
  */
19
- export function createTheme(theme: PartialDeep<Theme> = {}): Theme {
20
- return deepMerge<Theme, PartialDeep<Theme>>(defaultTheme, theme)
21
- }
15
+ export declare function createTheme(theme?: PartialDeep<Theme>): Theme;
@@ -0,0 +1,2 @@
1
+ import type { Theme } from './types';
2
+ export declare const defaultTheme: Theme;
@@ -0,0 +1,3 @@
1
+ export { createTheme } from './createTheme';
2
+ export { defaultTheme } from './defaultTheme';
3
+ export type { Theme } from './types';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("deepmerge"),e={screens:{sm:"640px",md:"768px",lg:"1024px",xl:"1280px","2xl":"1536px"},borderWidth:{none:"0",xs:"0.1rem",s:"0.2rem",m:"0.4rem"},colors:{transparent:"transparent",bg:{primary:"#f28133",primaryAccent:"#ad4c07",primarySubtle:"#fba56c",secondary:"#3481f2",secondaryAccent:"#074aad",secondarySubtle:"#4c93f7",body:"#fff"},bd:{primary:"#f28133",secondary:"#3481f2"},fg:{default:"#334155",accent:"#1161d7",cta:"#000",ctaInverse:"#fff"}},fontFamily:{openSans:'ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji"'},fontSize:{xs:"1rem",s:"1.2rem",m:"1.4rem",l:"1.6rem",xl:"1.8rem","2xl":"2rem","3xl":"2.4rem"},fontWeight:{regular:400,semibold:600,bold:700},lineHeight:{xs:"1.4rem",s:"1.7rem",m:"1.9rem",l:"2.2rem",xl:"2.4rem","2xl":"2.6rem","3xl":"2.8rem"},width:{pageMin:"320px",pageMax:"1066px"},borderRadius:{none:"0",xs:"0.4rem",s:"0.8rem",m:"1.6rem",l:"2.4rem",full:"100%"},boxShadow:{none:"none",normal:"0 -1px 4px 0 rgba(26, 26, 26, 0.08), 0 4px 8px 0 rgba(26, 26, 26, 0.12)",highlighted:"0 -1px 8px 0 rgba(26, 26, 26, 0.12), 0 4px 8px 0 rgba(0, 0, 0, 0.14)"},spacing:{auto:"auto",none:"0",xs:"0.4rem",s:"0.8rem",m:"1.6rem",l:"2.4rem",xl:"3.2rem",xxl:"4rem"},zIndex:{hide:-1,base:0,raised:1,dropdown:1e3,sticky:1100,overlay:1300,modal:1400,popover:1500,skipLink:1600,toast:1700,tooltip:1800}};exports.createTheme=function(r={}){return o(e,r)},exports.defaultTheme=e;
package/dist/index.mjs ADDED
@@ -0,0 +1,9 @@
1
+ import r from "deepmerge";
2
+ const o = { screens: { sm: "640px", md: "768px", lg: "1024px", xl: "1280px", "2xl": "1536px" }, borderWidth: { none: "0", xs: "0.1rem", s: "0.2rem", m: "0.4rem" }, colors: { transparent: "transparent", bg: { primary: "#f28133", primaryAccent: "#ad4c07", primarySubtle: "#fba56c", secondary: "#3481f2", secondaryAccent: "#074aad", secondarySubtle: "#4c93f7", body: "#fff" }, bd: { primary: "#f28133", secondary: "#3481f2" }, fg: { default: "#334155", accent: "#1161d7", cta: "#000", ctaInverse: "#fff" } }, fontFamily: { openSans: 'ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji"' }, fontSize: { xs: "1rem", s: "1.2rem", m: "1.4rem", l: "1.6rem", xl: "1.8rem", "2xl": "2rem", "3xl": "2.4rem" }, fontWeight: { regular: 400, semibold: 600, bold: 700 }, lineHeight: { xs: "1.4rem", s: "1.7rem", m: "1.9rem", l: "2.2rem", xl: "2.4rem", "2xl": "2.6rem", "3xl": "2.8rem" }, width: { pageMin: "320px", pageMax: "1066px" }, borderRadius: { none: "0", xs: "0.4rem", s: "0.8rem", m: "1.6rem", l: "2.4rem", full: "100%" }, boxShadow: { none: "none", normal: "0 -1px 4px 0 rgba(26, 26, 26, 0.08), 0 4px 8px 0 rgba(26, 26, 26, 0.12)", highlighted: "0 -1px 8px 0 rgba(26, 26, 26, 0.12), 0 4px 8px 0 rgba(0, 0, 0, 0.14)" }, spacing: { auto: "auto", none: "0", xs: "0.4rem", s: "0.8rem", m: "1.6rem", l: "2.4rem", xl: "3.2rem", xxl: "4rem" }, zIndex: { hide: -1, base: 0, raised: 1, dropdown: 1e3, sticky: 1100, overlay: 1300, modal: 1400, popover: 1500, skipLink: 1600, toast: 1700, tooltip: 1800 } };
3
+ function a(e = {}) {
4
+ return r(o, e);
5
+ }
6
+ export {
7
+ a as createTheme,
8
+ o as defaultTheme
9
+ };
@@ -0,0 +1,103 @@
1
+ export interface Theme {
2
+ screens: {
3
+ sm: string;
4
+ md: string;
5
+ lg: string;
6
+ xl: string;
7
+ '2xl': string;
8
+ };
9
+ borderWidth: {
10
+ none: string;
11
+ xs: string;
12
+ s: string;
13
+ m: string;
14
+ };
15
+ colors: {
16
+ transparent: string;
17
+ bg: {
18
+ body: string;
19
+ primary: string;
20
+ primaryAccent: string;
21
+ primarySubtle: string;
22
+ secondary: string;
23
+ secondaryAccent: string;
24
+ secondarySubtle: string;
25
+ };
26
+ fg: {
27
+ default: string;
28
+ accent: string;
29
+ cta: string;
30
+ ctaInverse: string;
31
+ };
32
+ bd: {
33
+ primary: string;
34
+ secondary: string;
35
+ };
36
+ };
37
+ fontFamily: {
38
+ openSans: string;
39
+ };
40
+ fontSize: {
41
+ xs: string;
42
+ s: string;
43
+ m: string;
44
+ l: string;
45
+ xl: string;
46
+ '2xl': string;
47
+ '3xl': string;
48
+ };
49
+ fontWeight: {
50
+ regular: number;
51
+ semibold: number;
52
+ bold: number;
53
+ };
54
+ lineHeight: {
55
+ xs: string;
56
+ s: string;
57
+ m: string;
58
+ l: string;
59
+ xl: string;
60
+ '2xl': string;
61
+ '3xl': string;
62
+ };
63
+ width: {
64
+ pageMin: string;
65
+ pageMax: string;
66
+ };
67
+ borderRadius: {
68
+ none: string;
69
+ xs: string;
70
+ s: string;
71
+ m: string;
72
+ l: string;
73
+ full: string;
74
+ };
75
+ boxShadow: {
76
+ none: string;
77
+ normal: string;
78
+ highlighted: string;
79
+ };
80
+ spacing: {
81
+ auto: string;
82
+ none: string;
83
+ xs: string;
84
+ s: string;
85
+ m: string;
86
+ l: string;
87
+ xl: string;
88
+ xxl: string;
89
+ };
90
+ zIndex: {
91
+ hide: number;
92
+ base: number;
93
+ raised: number;
94
+ dropdown: number;
95
+ sticky: number;
96
+ overlay: number;
97
+ modal: number;
98
+ popover: number;
99
+ skipLink: number;
100
+ toast: number;
101
+ tooltip: number;
102
+ };
103
+ }
package/package.json CHANGED
@@ -1,21 +1,19 @@
1
1
  {
2
2
  "name": "@spark-ui/theme-utils",
3
- "version": "1.1.1",
4
- "description": "Theme configuration",
3
+ "version": "2.0.0",
4
+ "description": "package for managing Spark theme configuration",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/index.mjs",
10
- "types": "./dist/index.d.mts",
10
+ "types": "./dist/index.d.ts",
11
11
  "scripts": {
12
12
  "build": "vite build"
13
13
  },
14
14
  "devDependencies": {
15
15
  "deepmerge": "4.3.0",
16
- "hex-rgb": "5.0.0",
17
- "parent-module": "3.0.0",
18
16
  "type-fest": "3.5.6"
19
17
  },
20
- "gitHead": "88ef74d7ffbf238395c021350fb6e54fe50bba43"
18
+ "gitHead": "a5a2528df9b68065d31be22132f60cc70d7fc472"
21
19
  }
package/tsconfig.json CHANGED
@@ -1,10 +1,4 @@
1
1
  {
2
2
  "extends": "../../../tsconfig.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "rootDir": "./",
6
- "outDir": "./dist",
7
- "declaration": true,
8
- },
9
- "include": ["**/*", "../../global.d.ts"]
3
+ "include": ["src/**/*", "../../global.d.ts"]
10
4
  }
package/vite.config.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import path from 'path'
2
2
  import { terser } from 'rollup-plugin-terser'
3
- import typescript from '@rollup/plugin-typescript'
3
+ import dts from 'vite-plugin-dts'
4
4
 
5
5
  const pkg = require(path.resolve(__dirname, './package.json'))
6
6
 
@@ -11,14 +11,18 @@ export default {
11
11
  build: {
12
12
  target: 'es2015',
13
13
  lib: {
14
- entry: 'index.mts',
14
+ entry: 'src/index.ts',
15
15
  formats: ['es', 'cjs'],
16
16
  fileName: 'index',
17
17
  },
18
18
  rollupOptions: {
19
- external: [...deps, ...devDeps, 'path', 'fs'],
20
- preserveModules: false,
21
- plugins: [terser(), typescript({ cacheDir: './dist' })],
19
+ external: [...deps, ...devDeps],
20
+ plugins: [terser()],
22
21
  },
23
22
  },
23
+ plugins: [
24
+ dts({
25
+ entryRoot: './src',
26
+ }),
27
+ ],
24
28
  }
@@ -1,127 +0,0 @@
1
- import { appendFileSync, existsSync, mkdirSync } from 'fs'
2
- import hexRgb from 'hex-rgb'
3
- import parentModule from 'parent-module'
4
- import { join } from 'path'
5
- import type { RequireAtLeastOne } from 'type-fest'
6
-
7
- import type { Theme } from './types.mjs'
8
- import {
9
- buildFilePath,
10
- isHex,
11
- isStringOrNumber,
12
- objectEntries,
13
- objectKeys,
14
- toKebabCase,
15
- } from './utils.mjs'
16
-
17
- /* eslint-disable-next-line @typescript-eslint/ban-types */
18
- type FlattenedTheme = Record<'className' | (string & {}), string | number>
19
- type NestedObj = Record<string, string | number | Record<string, string | number>>
20
-
21
- function flattenTheme(theme: Theme, className: string): FlattenedTheme {
22
- const flattenedTheme = {} as FlattenedTheme
23
-
24
- function flatten(obj: Theme, path?: string) {
25
- objectEntries(obj as unknown as NestedObj).forEach(([key, value]) => {
26
- if (value !== null && typeof value === 'object') {
27
- const formattedPath = path ? `--${path}-${key}` : `--${key}`
28
- flatten(value as unknown as Theme, toKebabCase(formattedPath.replace(/-{3,}/, '--')))
29
-
30
- return
31
- }
32
-
33
- if (isStringOrNumber(value)) {
34
- const getFormattedValue = () => {
35
- if (isHex(value)) {
36
- const { red, green, blue } = hexRgb(value)
37
-
38
- return `${red} ${green} ${blue}`
39
- }
40
-
41
- return value
42
- }
43
-
44
- flattenedTheme[`${path}-${toKebabCase(key as string)}`] = getFormattedValue()
45
- }
46
- })
47
- }
48
-
49
- flatten(theme)
50
-
51
- return {
52
- ...flattenedTheme,
53
- className,
54
- }
55
- }
56
-
57
- const toStringifiedTheme = (theme: Record<string, string | number>) =>
58
- Object.entries(theme)
59
- .map(([k, v]) => `${k}:${v}`)
60
- .join(';')
61
-
62
- const getStringifiedThemes = (themeRecord: Record<string, Theme>) =>
63
- objectKeys(themeRecord).map(key => {
64
- const { className, ...rest } = flattenTheme(themeRecord[key] as Theme, key)
65
-
66
- return key === 'default'
67
- ? `:root{${toStringifiedTheme(rest)}}`
68
- : `.${className}{${toStringifiedTheme(rest)}}`
69
- })
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
- ) {
97
- const { filepath, rootPath } = buildFilePath(join(parentModule() || '', path))
98
-
99
- const folders = filepath.split('/').slice(0, -1)
100
- folders.reduce((acc, folder) => {
101
- const folderPath = acc + folder + '/'
102
- if (!existsSync(folderPath)) {
103
- mkdirSync(folderPath)
104
- }
105
-
106
- return folderPath
107
- }, rootPath)
108
-
109
- try {
110
- appendFileSync(
111
- rootPath + filepath,
112
- `
113
- @tailwind base;
114
- @tailwind components;
115
- @tailwind utilities;
116
- @layer base {${getStringifiedThemes(themeRecord).join('')}}
117
- `,
118
- {
119
- flag: 'w',
120
- }
121
- )
122
-
123
- console.log(`✨ CSS tokens file has been created 👉 ${path}`)
124
- } catch (error) {
125
- console.error(error)
126
- }
127
- }
@@ -1,87 +0,0 @@
1
- import { existsSync, mkdirSync, writeFileSync } from 'fs'
2
- import parentModule from 'parent-module'
3
- import { join } from 'path'
4
-
5
- import { defaultTheme } from './defaultTheme.mjs'
6
- import type { Theme } from './types.mjs'
7
- import {
8
- buildFilePath,
9
- isHex,
10
- isStringOrNumber,
11
- objectEntries,
12
- toKebabCase,
13
- toKebabCaseKeys,
14
- } from './utils.mjs'
15
-
16
- type NestedObj = Record<string, string | number | Record<string, string | number>>
17
- type TailwindConfig = Record<string, Theme[keyof Theme]>
18
-
19
- function toTailwindConfig(theme: Theme): TailwindConfig {
20
- const themeCpy: Theme = JSON.parse(JSON.stringify(theme))
21
-
22
- function flatten(obj: Theme, path?: string) {
23
- objectEntries(obj as unknown as NestedObj).forEach(([key, value]) => {
24
- if (value !== null && typeof value === 'object') {
25
- const formattedPath = path ? `--${path}-${key}` : `--${key}`
26
- flatten(value as unknown as Theme, toKebabCase(formattedPath.replace(/-{3,}/, '--')))
27
-
28
- return
29
- }
30
-
31
- /* eslint-disable */
32
- if (isStringOrNumber(value)) {
33
- const formattedPath =
34
- /--colors/.test(path || '') && isHex(value)
35
- ? `rgb(var(${path}-${toKebabCase(key as string)}) / <alpha-value>)`
36
- : `var(${path}-${toKebabCase(key as string)})`
37
-
38
- /* @ts-ignore */
39
- obj[key as any] = formattedPath
40
- /* eslint-enable */
41
- }
42
- })
43
- }
44
-
45
- flatten(themeCpy)
46
-
47
- return toKebabCaseKeys(themeCpy)
48
- }
49
-
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) {
62
- const { filepath, rootPath } = buildFilePath(join(parentModule() || '', path))
63
-
64
- const folders = filepath.split('/').slice(0, -1)
65
- folders.reduce((acc, folder) => {
66
- const folderPath = acc + folder + '/'
67
- if (!existsSync(folderPath)) {
68
- mkdirSync(folderPath)
69
- }
70
-
71
- return folderPath
72
- }, rootPath)
73
-
74
- try {
75
- writeFileSync(
76
- rootPath + filepath,
77
- `module.exports = ${JSON.stringify(toTailwindConfig(defaultTheme))}`,
78
- {
79
- flag: 'w',
80
- }
81
- )
82
-
83
- console.log(`✨ Tailwind theme config file has been created 👉 ${path}`)
84
- } catch (error) {
85
- console.error(error)
86
- }
87
- }
package/defaultTheme.mts DELETED
@@ -1,106 +0,0 @@
1
- import type { Theme } from './types.mjs'
2
-
3
- export const defaultTheme: Theme = {
4
- screens: {
5
- sm: '640px',
6
- md: '768px',
7
- lg: '1024px',
8
- xl: '1280px',
9
- '2xl': '1536px',
10
- },
11
- borderWidth: {
12
- none: '0',
13
- xs: '0.1rem',
14
- s: '0.2rem',
15
- m: '0.4rem',
16
- },
17
- colors: {
18
- transparent: 'transparent',
19
- bg: {
20
- primary: '#f28133',
21
- primaryAccent: '#ad4c07',
22
- primarySubtle: '#fba56c',
23
- secondary: '#3481f2',
24
- secondaryAccent: '#074aad',
25
- secondarySubtle: '#4c93f7',
26
- body: '#fff',
27
- },
28
- bd: {
29
- primary: '#f28133',
30
- secondary: '#3481f2',
31
- },
32
- fg: {
33
- default: '#334155',
34
- accent: '#1161d7',
35
- cta: '#000',
36
- ctaInverse: '#fff',
37
- },
38
- },
39
- fontFamily: {
40
- openSans:
41
- 'ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji"',
42
- },
43
- fontSize: {
44
- xs: '1rem',
45
- s: '1.2rem',
46
- m: '1.4rem',
47
- l: '1.6rem',
48
- xl: '1.8rem',
49
- '2xl': '2rem',
50
- '3xl': '2.4rem',
51
- },
52
- fontWeight: {
53
- regular: 400,
54
- semibold: 600,
55
- bold: 700,
56
- },
57
- lineHeight: {
58
- xs: '1.4rem',
59
- s: '1.7rem',
60
- m: '1.9rem',
61
- l: '2.2rem',
62
- xl: '2.4rem',
63
- '2xl': '2.6rem',
64
- '3xl': '2.8rem',
65
- },
66
- width: {
67
- pageMin: '320px',
68
- pageMax: '1066px',
69
- },
70
- borderRadius: {
71
- none: '0',
72
- xs: '0.4rem',
73
- s: '0.8rem',
74
- m: '1.6rem',
75
- l: '2.4rem',
76
- full: '100%',
77
- },
78
- boxShadow: {
79
- none: 'none',
80
- normal: '0 -1px 4px 0 rgba(26, 26, 26, 0.08), 0 4px 8px 0 rgba(26, 26, 26, 0.12)',
81
- highlighted: '0 -1px 8px 0 rgba(26, 26, 26, 0.12), 0 4px 8px 0 rgba(0, 0, 0, 0.14)',
82
- },
83
- spacing: {
84
- auto: 'auto',
85
- none: '0',
86
- xs: '0.4rem',
87
- s: '0.8rem',
88
- m: '1.6rem',
89
- l: '2.4rem',
90
- xl: '3.2rem',
91
- xxl: '4rem',
92
- },
93
- zIndex: {
94
- hide: -1,
95
- base: 0,
96
- raised: 1,
97
- dropdown: 1000,
98
- sticky: 1100,
99
- overlay: 1300,
100
- modal: 1400,
101
- popover: 1500,
102
- skipLink: 1600,
103
- toast: 1700,
104
- tooltip: 1800,
105
- },
106
- }
package/index.mts DELETED
@@ -1,6 +0,0 @@
1
- export { createTheme } from './createTheme.mjs'
2
- export { createTailwindThemeConfigFile } from './createTailwindThemeConfigFile.mjs'
3
- export { createCSSTokensFile } from './createCSSTokensFile.mjs'
4
- export { defaultTheme } from './defaultTheme.mjs'
5
-
6
- export type { Theme } from './types.mjs'
package/index.stories.mdx DELETED
@@ -1,219 +0,0 @@
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/types.mts DELETED
@@ -1,103 +0,0 @@
1
- export interface Theme {
2
- screens: {
3
- sm: string
4
- md: string
5
- lg: string
6
- xl: string
7
- '2xl': string
8
- }
9
- borderWidth: {
10
- none: string
11
- xs: string
12
- s: string
13
- m: string
14
- }
15
- colors: {
16
- transparent: string
17
- bg: {
18
- body: string
19
- primary: string
20
- primaryAccent: string
21
- primarySubtle: string
22
- secondary: string
23
- secondaryAccent: string
24
- secondarySubtle: string
25
- }
26
- fg: {
27
- default: string
28
- accent: string
29
- cta: string
30
- ctaInverse: string
31
- }
32
- bd: {
33
- primary: string
34
- secondary: string
35
- }
36
- }
37
- fontFamily: {
38
- openSans: string
39
- }
40
- fontSize: {
41
- xs: string
42
- s: string
43
- m: string
44
- l: string
45
- xl: string
46
- '2xl': string
47
- '3xl': string
48
- }
49
- fontWeight: {
50
- regular: number
51
- semibold: number
52
- bold: number
53
- }
54
- lineHeight: {
55
- xs: string
56
- s: string
57
- m: string
58
- l: string
59
- xl: string
60
- '2xl': string
61
- '3xl': string
62
- }
63
- width: {
64
- pageMin: string
65
- pageMax: string
66
- }
67
- borderRadius: {
68
- none: string
69
- xs: string
70
- s: string
71
- m: string
72
- l: string
73
- full: string
74
- }
75
- boxShadow: {
76
- none: string
77
- normal: string
78
- highlighted: string
79
- }
80
- spacing: {
81
- auto: string
82
- none: string
83
- xs: string
84
- s: string
85
- m: string
86
- l: string
87
- xl: string
88
- xxl: string
89
- }
90
- zIndex: {
91
- hide: number
92
- base: number
93
- raised: number
94
- dropdown: number
95
- sticky: number
96
- overlay: number
97
- modal: number
98
- popover: number
99
- skipLink: number
100
- toast: number
101
- tooltip: number
102
- }
103
- }
package/utils.mts DELETED
@@ -1,71 +0,0 @@
1
- function objectKeys<T extends Record<string | number, unknown>>(obj: T): (keyof T)[] {
2
- return Object.keys(obj) as (keyof T)[]
3
- }
4
-
5
- function objectEntries<T extends Record<string | number, unknown>>(
6
- obj: T
7
- ): [keyof T, T[keyof T]][] {
8
- return Object.entries(obj) as [keyof T, T[keyof T]][]
9
- }
10
-
11
- function toKebabCase(v: string) {
12
- return v.replace(/[A-Z]/g, e => `-${e.toLocaleLowerCase()}`)
13
- }
14
-
15
- function isHex(value: string | number): value is string {
16
- if (typeof value === 'number') {
17
- return false
18
- }
19
-
20
- const regexp = /^#[0-9a-fA-F]+$/
21
-
22
- return regexp.test(value)
23
- }
24
-
25
- function isStringOrNumber(value: unknown): value is string | number {
26
- return typeof value === 'string' || typeof value === 'number'
27
- }
28
-
29
- // eslint-disable-next-line @typescript-eslint/ban-types
30
- function toKebabCaseKeys<T extends Object>(obj: T, level = 1): Record<string, T[keyof T]> {
31
- const result: any = {}
32
- for (const key in obj) {
33
- const value =
34
- typeof obj[key] === 'object' ? toKebabCaseKeys(obj[key] as T, level + 1) : obj[key]
35
- result[level > 1 ? key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() : key] = value
36
- }
37
-
38
- return result
39
- }
40
-
41
- function getFormattedPath(input: string) {
42
- const preFormat = input.slice(input.indexOf(':') + 1)
43
- const extensionRegex = /\/([^/]+)\.(jsx|js|tsx|ts|mjs|mts|cjs|cts)(?!$)/g
44
-
45
- return preFormat.replace(preFormat.match(extensionRegex)?.at(0) || '', '')
46
- }
47
-
48
- function buildFilePath(path: string) {
49
- let filepath = getFormattedPath(path).replace(/\\/g, '/')
50
-
51
- let rootPath = ''
52
- if (filepath[0] === '/') {
53
- rootPath = '/'
54
- filepath = filepath.slice(1)
55
- } else if (filepath[1] === ':') {
56
- rootPath = filepath.slice(0, 3)
57
- filepath = filepath.slice(3)
58
- }
59
-
60
- return { filepath, rootPath }
61
- }
62
-
63
- export {
64
- objectKeys,
65
- objectEntries,
66
- toKebabCase,
67
- isHex,
68
- toKebabCaseKeys,
69
- buildFilePath,
70
- isStringOrNumber,
71
- }