@trackunit/css-component-tokens 0.0.20 → 0.0.21

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.
Files changed (2) hide show
  1. package/index.js +256 -0
  2. package/package.json +4 -3
package/index.js ADDED
@@ -0,0 +1,256 @@
1
+ import { variablesToCSSRules } from '@trackunit/css-tailwind-custom-properties-plugin';
2
+ import plugin from 'tailwindcss/plugin';
3
+
4
+ /**
5
+ * Returns the designTokens passed, but ensures it satisfies DesignTokens type while keeping it literal
6
+ */
7
+ const AsComponentTokenDefinition = (designTokens) => designTokens;
8
+
9
+ /*
10
+ * This file holds the custom design tokens for this component.
11
+ * These will be added to the tailwind configuration.
12
+ */
13
+ const BaseInputTokenDefinition = AsComponentTokenDefinition({
14
+ DEFAULT: {
15
+ border: { borderRadius: "var(--border-radius-lg)", borderWidth: "1px" },
16
+ shadow: { boxShadow: "var(--box-shadow-sm)" },
17
+ background: { backgroundColor: "white" },
18
+ prefix: { borderRightWidth: "1px" },
19
+ },
20
+ "theme-marketing": {
21
+ border: { borderRadius: "70px", borderWidth: "0px" },
22
+ shadow: { boxShadow: "none" },
23
+ background: { backgroundColor: "rgb(var(--color-stone-50))" },
24
+ prefix: { borderRightWidth: "0px" },
25
+ },
26
+ });
27
+
28
+ /*
29
+ * This file holds the custom design tokens for this component.
30
+ * These will be added to the tailwind configuration.
31
+ */
32
+ const ButtonTokenDefinition = AsComponentTokenDefinition({
33
+ DEFAULT: {
34
+ "secondary-color": { backgroundColor: "rgb(var(--color-secondary-200))", color: "rgb(var(--color-secondary-600))" },
35
+ border: { borderRadius: "var(--border-radius-lg)" },
36
+ height: { height: "auto" },
37
+ padding: { paddingLeft: "var(--spacing-2-5)", paddingRight: "var(--spacing-2-5)" },
38
+ },
39
+ "theme-marketing": {
40
+ "secondary-color": { backgroundColor: "rgb(var(--color-secondary-600))", color: "rgb(var(--color-secondary-50))" },
41
+ border: { borderRadius: "50px" },
42
+ height: { height: "55px" },
43
+ padding: { paddingLeft: "var(--spacing-12)", paddingRight: "var(--spacing-12)" },
44
+ },
45
+ });
46
+
47
+ /*
48
+ * This file holds the custom design tokens for this component.
49
+ * These will be added to the tailwind configuration.
50
+ */
51
+ const CardTokenDefinition = AsComponentTokenDefinition({
52
+ DEFAULT: {
53
+ border: { borderColor: "rgb(var(--color-slate-200))", borderRadius: "var(--border-radius-DEFAULT)" },
54
+ "popover-border": { borderRadius: "var(--border-radius-DEFAULT)" },
55
+ shadow: { boxShadow: "var(--box-shadow-DEFAULT)" },
56
+ spacing: { gap: "var(--spacing-3)", padding: "var(--spacing-3)" },
57
+ "spacing-sm": { gap: "var(--spacing-4)", padding: "var(--spacing-4)" },
58
+ "spacing-md": { gap: "var(--spacing-4)", padding: "var(--spacing-6)" },
59
+ },
60
+ "theme-marketing": {
61
+ border: { borderColor: "rgba(0, 0, 0, 0)", borderRadius: "2rem" },
62
+ "popover-border": { borderRadius: "1rem" },
63
+ shadow: { boxShadow: "0px 20px 48px rgba(73, 55, 54, 0.1)" },
64
+ spacing: { gap: "var(--spacing-1)", padding: "var(--spacing-1)" },
65
+ "spacing-sm": { gap: "var(--spacing-1)", padding: "var(--spacing-1)" },
66
+ "spacing-md": { gap: "var(--spacing-1)", padding: "var(--spacing-1)" },
67
+ },
68
+ });
69
+
70
+ /*
71
+ * This file holds the custom design tokens for this component.
72
+ * These will be added to the tailwind configuration.
73
+ */
74
+ const FormGroupTokenDefinition = AsComponentTokenDefinition({
75
+ DEFAULT: {
76
+ gap: { display: "auto", gap: "auto" },
77
+ font: { fontWeight: "normal" },
78
+ },
79
+ "theme-marketing": {
80
+ gap: { display: "grid", gap: "var(--spacing-4)" },
81
+ font: { fontWeight: "700" },
82
+ },
83
+ });
84
+
85
+ /*
86
+ * This file holds the custom design tokens for this component.
87
+ * These will be added to the tailwind configuration.
88
+ */
89
+ const PopoverTokenDefinition = AsComponentTokenDefinition({
90
+ DEFAULT: {
91
+ border: { borderRadius: "var(--border-radius-DEFAULT)" },
92
+ },
93
+ "theme-marketing": {
94
+ border: { borderRadius: "1rem" },
95
+ },
96
+ });
97
+
98
+ /*
99
+ * This file holds the custom design tokens for this component.
100
+ * These will be added to the tailwind configuration.
101
+ */
102
+ const PopoverListItemTokenDefinition = AsComponentTokenDefinition({
103
+ DEFAULT: {
104
+ border: { borderRadius: "var(--border-radius-DEFAULT)" },
105
+ },
106
+ "theme-marketing": {
107
+ border: { borderRadius: "1rem" },
108
+ },
109
+ });
110
+
111
+ /*
112
+ * This file holds the custom design tokens for this component.
113
+ * These will be added to the tailwind configuration.
114
+ */
115
+ const SearchTokenDefinition = AsComponentTokenDefinition({
116
+ DEFAULT: {
117
+ width: { width: "9rem" },
118
+ background: { backgroundColor: "" },
119
+ borderless: { borderStyle: "hidden" },
120
+ "focus-hover": {
121
+ borderWidth: "1px",
122
+ borderBottomWidth: "1px",
123
+ borderColor: "rgb(var(--color-gray-400))",
124
+ borderBottomColor: "rgb(var(--color-gray-400))",
125
+ backgroundColor: "rgb(var(--color-slate-50))",
126
+ },
127
+ border: {
128
+ borderStyle: "solid",
129
+ borderWidth: "1px",
130
+ borderBottomWidth: "1px",
131
+ borderColor: "rgb(var(--color-gray-200))",
132
+ borderBottomColor: "rgb(var(--color-gray-200))",
133
+ borderRadius: "0.5rem",
134
+ },
135
+ "focus-within": {
136
+ borderColor: "rgb(var(--color-slate-400))",
137
+ boxShadow: "var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000)",
138
+ },
139
+ widen: { borderBottomColor: "rgb(var(--color-gray-400))" },
140
+ "widen-focus": { borderBottomColor: "rgb(var(--color-gray-400))" },
141
+ prefix: {
142
+ color: "rgb(var(--color-slate-400))",
143
+ cursor: "default",
144
+ },
145
+ },
146
+ "theme-marketing": {
147
+ width: { width: "0px" },
148
+ background: { backgroundColor: "transparent" },
149
+ borderless: {
150
+ borderStyle: "hidden",
151
+ },
152
+ "focus-hover": {
153
+ borderWidth: "0px",
154
+ borderBottomWidth: "2px",
155
+ borderColor: "rgb(var(--color-black-DEFAULT))",
156
+ borderBottomColor: "rgb(var(--color-black-DEFAULT))",
157
+ backgroundColor: "transparent",
158
+ },
159
+ border: {
160
+ borderStyle: "solid",
161
+ borderWidth: "0px",
162
+ borderBottomWidth: "2px",
163
+ borderColor: "rgb(var(--color-white-DEFAULT))",
164
+ borderBottomColor: "rgb(var(--color-black-DEFAULT))",
165
+ borderRadius: "0px",
166
+ },
167
+ "focus-within": {
168
+ borderColor: "none",
169
+ boxShadow: "none",
170
+ },
171
+ widen: {
172
+ borderBottomColor: "rgb(var(--color-white-DEFAULT))",
173
+ },
174
+ "widen-focus": {
175
+ borderBottomColor: "rgb(var(--color-black-DEFAULT))",
176
+ },
177
+ prefix: {
178
+ color: "rgb(var(--color-slate-600))",
179
+ cursor: "pointer",
180
+ },
181
+ },
182
+ });
183
+
184
+ const componentTokenDefinitions = {
185
+ card: CardTokenDefinition,
186
+ button: ButtonTokenDefinition,
187
+ search: SearchTokenDefinition,
188
+ popover: PopoverTokenDefinition,
189
+ popoverListItem: PopoverListItemTokenDefinition,
190
+ baseInput: BaseInputTokenDefinition,
191
+ formGroup: FormGroupTokenDefinition,
192
+ };
193
+
194
+ /** Returns object structure for Tailwind variable config for specified theme for ALL ComponentTokenDefinitions. */
195
+ const getComponentTokenVariablesByThemeName = (theme, componentTokenDefinitions) => {
196
+ const themeTokenObject = Object.fromEntries(Object.entries(componentTokenDefinitions).map(([componentName, componentThemes]) => [
197
+ componentName,
198
+ componentThemes[theme],
199
+ ]));
200
+ return themeTokenObject;
201
+ };
202
+
203
+ /** The prefix for css variables and tailwind component classes */
204
+ const PREFIX = "component";
205
+ /**
206
+ * Get the css variable value for the component tokens specific property.
207
+ *
208
+ * @param component The name of the component to get the variable for.
209
+ * @param token The component-level token name
210
+ * @param cssProperty The css property on the component-level Token
211
+ * @returns {string} `var(--${PREFIX}-${component}-${token}-${cssProperty})`
212
+ */
213
+ const componentTokenAsCssVariableString = (component, token, cssProperty) => `var(--${PREFIX}-${component}-${token}-${cssProperty})`;
214
+ /**
215
+ * Get the full CSSProperties for a component token.
216
+ * Replaces the css values from the ComponentTokenDefinition with the corresponding css variable.
217
+ *
218
+ * @param {ComponentNames} component The name of the component
219
+ * @param {ComponentTokenNames} token The name of the token
220
+ * @param {CSSProperties} tokenObject The CSSProperties for the component token
221
+ * @returns {*} {CSSProperties} CSSProperties converted to use the css variable for the correstonding token
222
+ */
223
+ const componentTokenToCSSProperties = (component, token, tokenObject) => {
224
+ return Object.fromEntries(Object.keys(tokenObject).map(key => [
225
+ key,
226
+ componentTokenAsCssVariableString(component, token, key),
227
+ ]));
228
+ };
229
+ /**
230
+ * Generate the full object og componentToken rules for the Tailwind plugin.
231
+ */
232
+ const getAllComponentTokenRules = (componentDefinitions) => {
233
+ const components = {};
234
+ Object.entries(componentDefinitions).forEach(([componentName, themes]) => Object.entries(themes.DEFAULT).forEach(([tokenName, value]) => {
235
+ const typedComponentName = componentName; // TS does not do literals from Object keys so we do it manually
236
+ const typedTokenName = tokenName; // TS does not do literals from Object keys so we do it manually
237
+ const selector = `.${PREFIX}-${typedComponentName}-${typedTokenName}`;
238
+ components[selector] = componentTokenToCSSProperties(typedComponentName, typedTokenName, value);
239
+ }));
240
+ return components;
241
+ };
242
+
243
+ const cssVariablesDefinition = variablesToCSSRules({
244
+ DEFAULT: {
245
+ component: getComponentTokenVariablesByThemeName("DEFAULT", componentTokenDefinitions),
246
+ },
247
+ "theme-marketing": {
248
+ component: getComponentTokenVariablesByThemeName("theme-marketing", componentTokenDefinitions),
249
+ },
250
+ });
251
+ const ComponentTokensPlugin = plugin(function ({ addComponents }) {
252
+ addComponents(cssVariablesDefinition);
253
+ addComponents(getAllComponentTokenRules(componentTokenDefinitions));
254
+ });
255
+
256
+ export { ComponentTokensPlugin, ComponentTokensPlugin as default };
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@trackunit/css-component-tokens",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "main": "./index.cjs",
5
5
  "repository": "https://github.com/Trackunit/manager",
6
6
  "license": "SEE LICENSE IN LICENSE.txt",
7
- "type": "commonjs",
7
+ "module": "./index.js",
8
+ "type": "module",
8
9
  "types": "./src/index.d.ts",
9
10
  "dependencies": {
10
11
  "csstype": "3.1.1",
11
- "@trackunit/css-tailwind-custom-properties-plugin": "0.0.12"
12
+ "@trackunit/css-tailwind-custom-properties-plugin": "0.0.13"
12
13
  },
13
14
  "peerDependencies": {}
14
15
  }