vueless 0.0.696 → 0.0.697

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/package.json +1 -1
  2. package/utils/theme.ts +97 -52
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vueless",
3
- "version": "0.0.696",
3
+ "version": "0.0.697",
4
4
  "license": "MIT",
5
5
  "description": "Vue Styleless UI Component Library, powered by Tailwind CSS.",
6
6
  "keywords": [
package/utils/theme.ts CHANGED
@@ -37,6 +37,19 @@ interface Colors extends DefaultColors {
37
37
  [key: string]: Partial<TailwindColorShades> | string;
38
38
  }
39
39
 
40
+ declare interface RootCSSVariableOptions {
41
+ colors: Colors;
42
+ brand: string;
43
+ gray: string;
44
+ ring: number;
45
+ ringOffset: number;
46
+ ringOffsetColorDark: string;
47
+ ringOffsetColorLight: string;
48
+ roundingSm: number;
49
+ rounding: number;
50
+ roundingLg: number;
51
+ }
52
+
40
53
  export function themeInit() {
41
54
  if (isSSR) return;
42
55
 
@@ -107,6 +120,28 @@ export function getSelectedGrayColor() {
107
120
  return (isCSR && localStorage.getItem("gray")) || undefined;
108
121
  }
109
122
 
123
+ export function convertHexInRgb(hex?: string) {
124
+ if (!hex) return;
125
+
126
+ const color = hex.replace(/#/g, "");
127
+
128
+ let r, g, b;
129
+
130
+ if (color.length === 6) {
131
+ r = parseInt(color.substring(0, 2), 16);
132
+ g = parseInt(color.substring(2, 4), 16);
133
+ b = parseInt(color.substring(4, 6), 16);
134
+ }
135
+
136
+ if (color.length === 3) {
137
+ r = parseInt(color.substring(0, 1).repeat(2), 16);
138
+ g = parseInt(color.substring(1, 2).repeat(2), 16);
139
+ b = parseInt(color.substring(2, 3).repeat(2), 16);
140
+ }
141
+
142
+ return color.length === 6 || color.length === 3 ? `${r}, ${g}, ${b}` : "";
143
+ }
144
+
110
145
  export function setTheme(config: Config = {}) {
111
146
  setColorMode(vuelessConfig.colorMode || config.colorMode || ColorMode.Light);
112
147
 
@@ -119,7 +154,7 @@ export function setTheme(config: Config = {}) {
119
154
  let brand: BrandColors =
120
155
  config.brand ?? getSelectedBrandColor() ?? vuelessConfig.brand ?? DEFAULT_BRAND_COLOR;
121
156
 
122
- const gray: GrayColors =
157
+ let gray: GrayColors =
123
158
  config.gray ?? getSelectedGrayColor() ?? vuelessConfig.gray ?? DEFAULT_GRAY_COLOR;
124
159
 
125
160
  const ring = config.ring ?? vuelessConfig.ring ?? DEFAULT_RING;
@@ -135,11 +170,6 @@ export function setTheme(config: Config = {}) {
135
170
  vuelessConfig.ringOffsetColorLight ??
136
171
  DEFAULT_RING_OFFSET_COLOR_LIGHT;
137
172
 
138
- const isDarkMode = isCSR && document.documentElement.classList.contains(DARK_MODE_SELECTOR);
139
- const defaultRingOffsetColor = isDarkMode ? ringOffsetColorDark : ringOffsetColorLight;
140
- const defaultBrandShade = isDarkMode ? 400 : 600;
141
- const defaultGrayShade = isDarkMode ? 400 : 600;
142
-
143
173
  isCSR && localStorage.setItem("brand", brand);
144
174
  isCSR && localStorage.setItem("gray", gray);
145
175
 
@@ -160,13 +190,74 @@ export function setTheme(config: Config = {}) {
160
190
  if (!isBrandColor) {
161
191
  // eslint-disable-next-line no-console
162
192
  console.warn(`The brand color '${brand}' is missing in your palette.`);
193
+
194
+ brand = DEFAULT_GRAY_COLOR;
163
195
  }
164
196
 
165
197
  if (!isGrayColor) {
166
198
  // eslint-disable-next-line no-console
167
199
  console.warn(`The gray color '${gray}' is missing in your palette.`);
200
+
201
+ gray = DEFAULT_GRAY_COLOR;
202
+ }
203
+
204
+ return setRootCSSVariables({
205
+ colors,
206
+ brand,
207
+ gray,
208
+ ring,
209
+ ringOffset,
210
+ ringOffsetColorDark,
211
+ ringOffsetColorLight,
212
+ roundingSm,
213
+ rounding,
214
+ roundingLg,
215
+ });
216
+ }
217
+
218
+ function getRoundings(sm?: number, md?: number, lg?: number) {
219
+ const rounding = Math.max(0, md ?? DEFAULT_ROUNDING);
220
+ let roundingSm = Math.max(0, rounding - ROUNDING_DECREMENT);
221
+ let roundingLg = Math.max(0, rounding + ROUNDING_INCREMENT);
222
+
223
+ if (rounding === ROUNDING_INCREMENT) {
224
+ roundingSm = ROUNDING_DECREMENT;
225
+ }
226
+
227
+ if (rounding === ROUNDING_DECREMENT) {
228
+ roundingSm = ROUNDING_INCREMENT - ROUNDING_DECREMENT;
229
+ }
230
+
231
+ if (rounding === 0) {
232
+ roundingLg = ROUNDING_INCREMENT - ROUNDING_DECREMENT;
168
233
  }
169
234
 
235
+ return {
236
+ rounding,
237
+ roundingSm: sm === undefined ? roundingSm : Math.max(0, sm),
238
+ roundingLg: lg === undefined ? roundingLg : Math.max(0, lg),
239
+ };
240
+ }
241
+
242
+ function setRootCSSVariables(options: RootCSSVariableOptions) {
243
+ const {
244
+ colors,
245
+ brand,
246
+ gray,
247
+ ring,
248
+ ringOffset,
249
+ ringOffsetColorDark,
250
+ ringOffsetColorLight,
251
+ roundingSm,
252
+ rounding,
253
+ roundingLg,
254
+ } = options;
255
+
256
+ const isDarkMode = isCSR && document.documentElement.classList.contains(DARK_MODE_SELECTOR);
257
+ const defaultRingOffsetColor = isDarkMode ? ringOffsetColorDark : ringOffsetColorLight;
258
+ const defaultBrandShade = isDarkMode ? 400 : 600;
259
+ const defaultGrayShade = isDarkMode ? 400 : 600;
260
+
170
261
  const variables: Partial<VuelessCssVariables> = {
171
262
  "--vl-rounding-sm": `${Number(roundingSm) / PX_IN_REM}rem`,
172
263
  "--vl-rounding": `${Number(rounding) / PX_IN_REM}rem`,
@@ -209,49 +300,3 @@ export function setTheme(config: Config = {}) {
209
300
 
210
301
  return rootVariables;
211
302
  }
212
-
213
- export function convertHexInRgb(hex?: string) {
214
- if (!hex) return;
215
-
216
- const color = hex.replace(/#/g, "");
217
-
218
- let r, g, b;
219
-
220
- if (color.length === 6) {
221
- r = parseInt(color.substring(0, 2), 16);
222
- g = parseInt(color.substring(2, 4), 16);
223
- b = parseInt(color.substring(4, 6), 16);
224
- }
225
-
226
- if (color.length === 3) {
227
- r = parseInt(color.substring(0, 1).repeat(2), 16);
228
- g = parseInt(color.substring(1, 2).repeat(2), 16);
229
- b = parseInt(color.substring(2, 3).repeat(2), 16);
230
- }
231
-
232
- return color.length === 6 || color.length === 3 ? `${r}, ${g}, ${b}` : "";
233
- }
234
-
235
- function getRoundings(sm?: number, md?: number, lg?: number) {
236
- const rounding = Math.max(0, md ?? DEFAULT_ROUNDING);
237
- let roundingSm = Math.max(0, rounding - ROUNDING_DECREMENT);
238
- let roundingLg = Math.max(0, rounding + ROUNDING_INCREMENT);
239
-
240
- if (rounding === ROUNDING_INCREMENT) {
241
- roundingSm = ROUNDING_DECREMENT;
242
- }
243
-
244
- if (rounding === ROUNDING_DECREMENT) {
245
- roundingSm = ROUNDING_INCREMENT - ROUNDING_DECREMENT;
246
- }
247
-
248
- if (rounding === 0) {
249
- roundingLg = ROUNDING_INCREMENT - ROUNDING_DECREMENT;
250
- }
251
-
252
- return {
253
- rounding,
254
- roundingSm: sm === undefined ? roundingSm : Math.max(0, sm),
255
- roundingLg: lg === undefined ? roundingLg : Math.max(0, lg),
256
- };
257
- }