@bobbykim/manguito-theme 0.1.4 → 0.3.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/theme/index.ts ADDED
@@ -0,0 +1,658 @@
1
+ import MCLTheme from './theme.js'
2
+ import type {
3
+ ColorPalette,
4
+ HeadingSize,
5
+ BodyText,
6
+ FontWeight,
7
+ Range,
8
+ OpacityRange,
9
+ SpacingLevel,
10
+ Position,
11
+ } from './theme.types'
12
+
13
+ /**
14
+ * @TailwindCSS - Good practice is to let all of used classes to be in full instead of generated dynamically using methods such as string literal.
15
+ * @Interface - ClassType to define the case, and Input type to generate tailwind clssname.
16
+ * @Export GenerateMCLClass - extends MCLTheme class
17
+ */
18
+
19
+ export const colorType = [
20
+ 'BGCOLOR',
21
+ 'BEFOREBG',
22
+ 'AFTERBG',
23
+ 'HVBGCOLOR',
24
+ 'ACTIVEBG',
25
+ 'TEXTCOLOR',
26
+ 'HVTEXTCOLOR',
27
+ 'DISABLEDTEXTCOLOR',
28
+ 'SVGFILL',
29
+ 'RINGCOLOR',
30
+ 'FOCUSRING',
31
+ 'ACTIVERING',
32
+ 'OFFSETRING',
33
+ 'DUMMYCOLOR',
34
+ 'BTNCOLOR',
35
+ 'LISTCOLOR',
36
+ 'BORDER',
37
+ 'BORDERX',
38
+ 'BORDERY',
39
+ 'BORDERT',
40
+ 'BORDERB',
41
+ 'BORDERL',
42
+ 'BORDERR',
43
+ ] as const
44
+
45
+ export const textType = [
46
+ 'H1',
47
+ 'H2',
48
+ 'H3',
49
+ 'H4',
50
+ 'BODYTEXT',
51
+ 'FONTWEIGHT',
52
+ 'TEXTALIGN',
53
+ ] as const
54
+ export const borderWidthType = [
55
+ 'BORDERW',
56
+ 'BORDERXW',
57
+ 'BORDERYW',
58
+ 'BORDERTW',
59
+ 'BORDERBW',
60
+ 'BORDERLW',
61
+ 'BORDERRW',
62
+ ] as const
63
+ export const scaleType = ['BGOPACITY', 'OPACITY'] as const
64
+ export const spacingType = [
65
+ 'MARGIN',
66
+ 'MARGINX',
67
+ 'MARGINY',
68
+ 'MARGINT',
69
+ 'MARGINB',
70
+ 'MARGINL',
71
+ 'MARGINR',
72
+ 'PADDING',
73
+ 'PADDINGX',
74
+ 'PADDINGY',
75
+ 'PADDINGT',
76
+ 'PADDINGB',
77
+ 'PADDINGL',
78
+ 'PADDINGR',
79
+ 'GAP',
80
+ ] as const
81
+ const allType = [
82
+ ...colorType,
83
+ ...textType,
84
+ ...borderWidthType,
85
+ ...scaleType,
86
+ ...spacingType,
87
+ ] as const
88
+
89
+ export type ClassType = (typeof allType)[number]
90
+ export type ColorClassType = (typeof colorType)[number]
91
+ export type TextClassType = (typeof textType)[number]
92
+ export type BorderWidthClassType = (typeof borderWidthType)[number]
93
+ export type ScaleClassType = (typeof scaleType)[number]
94
+ export type SpacingClassType = (typeof spacingType)[number]
95
+
96
+ export type InputType =
97
+ | ColorPalette
98
+ | HeadingSize
99
+ | BodyText
100
+ | FontWeight
101
+ | Range<1, 12>
102
+ | OpacityRange
103
+ | SpacingLevel
104
+ | Position
105
+
106
+ class GenerateMCLClass extends MCLTheme {
107
+ classType: ClassType
108
+ classValue: InputType
109
+
110
+ constructor(classType: ClassType, classValue: InputType) {
111
+ super()
112
+ this.classType = classType
113
+ this.classValue = classValue
114
+ }
115
+ // handle color classes types
116
+ generateColorClass(): string {
117
+ switch (this.classType) {
118
+ case 'BGCOLOR':
119
+ return this.getBgColorClass()
120
+ case 'HVBGCOLOR':
121
+ return this.getHoverBgColorClass()
122
+ case 'BEFOREBG':
123
+ return this.getBeforeBgColorClass()
124
+ case 'AFTERBG':
125
+ return this.getAfterBgColorClass()
126
+ case 'ACTIVEBG':
127
+ return this.getActiveBgColorClass()
128
+ case 'TEXTCOLOR':
129
+ return this.getTextColorClass()
130
+ case 'HVTEXTCOLOR':
131
+ return this.getHoverTextColorClass()
132
+ case 'DISABLEDTEXTCOLOR':
133
+ return this.getDisabledTextColorClass()
134
+ case 'SVGFILL':
135
+ return this.getSvgFillColorClass()
136
+ case 'RINGCOLOR':
137
+ return this.getRingColorClass()
138
+ case 'FOCUSRING':
139
+ return this.getFocusRingColorClass()
140
+ case 'ACTIVERING':
141
+ return this.getActiveRingColorClass()
142
+ case 'OFFSETRING':
143
+ return this.getRingOffsetColorClass()
144
+ case 'BTNCOLOR':
145
+ return this.getBtnColorClass()
146
+ case 'LISTCOLOR':
147
+ return this.getRingColorClass()
148
+ case 'BORDER':
149
+ return this.getBorderColorClass()
150
+ case 'BORDERX':
151
+ return this.getBorderXColorClass()
152
+ case 'BORDERY':
153
+ return this.getBorderYColorClass()
154
+ case 'BORDERT':
155
+ return this.getBorderTopColorClass()
156
+ case 'BORDERB':
157
+ return this.getBorderBottomColorClass()
158
+ case 'BORDERL':
159
+ return this.getBorderLeftColorClass()
160
+ case 'BORDERR':
161
+ return this.getBorderRightColorClass()
162
+ default:
163
+ return ' '
164
+ }
165
+ }
166
+ // handle text class types
167
+ generateTextClass(): string {
168
+ switch (this.classType) {
169
+ // case: heading text class
170
+ case 'H1':
171
+ return this.getH1Class()
172
+ case 'H2':
173
+ return this.getH2Class()
174
+ case 'H3':
175
+ return this.getH3Class()
176
+ case 'H4':
177
+ return this.getH4Class()
178
+ // case: body text class
179
+ case 'BODYTEXT':
180
+ return this.getBodyTextSizeClass()
181
+ // case: font weight class
182
+ case 'FONTWEIGHT':
183
+ return this.getFontWeightClass()
184
+ // case: text alignment class
185
+ case 'TEXTALIGN':
186
+ return this.getTextAlignClass()
187
+ default:
188
+ return ' '
189
+ }
190
+ }
191
+ // handle border width class types
192
+ generateBorderWidthClass(): string {
193
+ switch (this.classType) {
194
+ // case: border width class
195
+ case 'BORDERW':
196
+ return this.getBorderWidthClass()
197
+ case 'BORDERXW':
198
+ return this.getBorderXWidthClass()
199
+ case 'BORDERYW':
200
+ return this.getBorderYWidthClass()
201
+ case 'BORDERTW':
202
+ return this.getBorderTopWidthClass()
203
+ case 'BORDERBW':
204
+ return this.getBorderBottomWidthClass()
205
+ case 'BORDERLW':
206
+ return this.getBorderLeftWidthClass()
207
+ case 'BORDERRW':
208
+ return this.getBorderWidthClass()
209
+ default:
210
+ return ' '
211
+ }
212
+ }
213
+ generateScaleClass(): string {
214
+ switch (this.classType) {
215
+ // case: opacity class
216
+ case 'OPACITY':
217
+ return this.getOpacityClass()
218
+ case 'BGOPACITY':
219
+ return this.getBgOpacityClass()
220
+ default:
221
+ return ' '
222
+ }
223
+ }
224
+ generateSpacingClass(): string {
225
+ switch (this.classType) {
226
+ // case: margins
227
+ case 'MARGIN':
228
+ return this.getMarginClass()
229
+ case 'MARGINX':
230
+ return this.getMarginXClass()
231
+ case 'MARGINY':
232
+ return this.getMarginYClass()
233
+ case 'MARGINT':
234
+ return this.getMarginTopClass()
235
+ case 'MARGINB':
236
+ return this.getMarginBottomClass()
237
+ case 'MARGINL':
238
+ return this.getMarginLeftClass()
239
+ case 'MARGINR':
240
+ return this.getMarginRightClass()
241
+ // case: paddings
242
+ case 'PADDING':
243
+ return this.getPaddingClass()
244
+ case 'PADDINGX':
245
+ return this.getPaddingXClass()
246
+ case 'PADDINGY':
247
+ return this.getPaddingYClass()
248
+ case 'PADDINGT':
249
+ return this.getPaddingTopClass()
250
+ case 'PADDINGB':
251
+ return this.getPaddingBottomClass()
252
+ case 'PADDINGL':
253
+ return this.getPaddingLeftClass()
254
+ case 'PADDINGR':
255
+ return this.getPaddingRightClass()
256
+ // case: gap
257
+ case 'GAP':
258
+ return this.getGapClass()
259
+ default:
260
+ return ' '
261
+ }
262
+ }
263
+ // handle color class
264
+ // bg color
265
+ getBgColorClass(): string {
266
+ if (!this.validateColorType) {
267
+ return ' '
268
+ }
269
+ return MCLTheme.bgColor[this.classValue as ColorPalette]
270
+ }
271
+ getHoverBgColorClass(): string {
272
+ if (!this.validateColorType) {
273
+ return ' '
274
+ }
275
+ return MCLTheme.hoverBgColor[this.classValue as ColorPalette]
276
+ }
277
+ getBeforeBgColorClass(): string {
278
+ if (!this.validateColorType) {
279
+ return ' '
280
+ }
281
+ return MCLTheme.beforeBgColor[this.classValue as ColorPalette]
282
+ }
283
+ getAfterBgColorClass(): string {
284
+ if (!this.validateColorType) {
285
+ return ' '
286
+ }
287
+ return MCLTheme.afterBgColor[this.classValue as ColorPalette]
288
+ }
289
+ getActiveBgColorClass(): string {
290
+ if (!this.validateColorType) {
291
+ return ' '
292
+ }
293
+ return MCLTheme.bgActiveColor[this.classValue as ColorPalette]
294
+ }
295
+ // text color
296
+ getTextColorClass(): string {
297
+ if (!this.validateColorType) {
298
+ return ' '
299
+ }
300
+ return MCLTheme.textColor[this.classValue as ColorPalette]
301
+ }
302
+ getHoverTextColorClass(): string {
303
+ if (!this.validateColorType) {
304
+ return ' '
305
+ }
306
+ return MCLTheme.hoverTextColor[this.classValue as ColorPalette]
307
+ }
308
+ getDisabledTextColorClass(): string {
309
+ if (!this.validateColorType) {
310
+ return ' '
311
+ }
312
+ return MCLTheme.disabledTextColor[this.classValue as ColorPalette]
313
+ }
314
+ // svg color
315
+ getSvgFillColorClass(): string {
316
+ if (!this.validateColorType) {
317
+ return ' '
318
+ }
319
+ return MCLTheme.svgFillColor[this.classValue as ColorPalette]
320
+ }
321
+ // ring color
322
+ getRingColorClass(): string {
323
+ if (!this.validateColorType) {
324
+ return ' '
325
+ }
326
+ return MCLTheme.ringColor[this.classValue as ColorPalette]
327
+ }
328
+ getFocusRingColorClass(): string {
329
+ if (!this.validateColorType) {
330
+ return ' '
331
+ }
332
+ return MCLTheme.ringFocusColor[this.classValue as ColorPalette]
333
+ }
334
+ getActiveRingColorClass(): string {
335
+ if (!this.validateColorType) {
336
+ return ' '
337
+ }
338
+ return MCLTheme.ringActiveColor[this.classValue as ColorPalette]
339
+ }
340
+ getRingOffsetColorClass(): string {
341
+ if (!this.validateColorType) {
342
+ return ' '
343
+ }
344
+ return MCLTheme.ringOffsetColor[this.classValue as ColorPalette]
345
+ }
346
+ // btn & list
347
+ getBtnColorClass(): string {
348
+ if (!this.validateColorType) {
349
+ return ' '
350
+ }
351
+ return MCLTheme.btnColor[this.classValue as ColorPalette]
352
+ }
353
+ getListColorClass(): string {
354
+ if (!this.validateColorType) {
355
+ return ' '
356
+ }
357
+ return MCLTheme.listColor[this.classValue as ColorPalette]
358
+ }
359
+ // border color
360
+ getBorderColorClass(): string {
361
+ if (!this.validateColorType) {
362
+ return ' '
363
+ }
364
+ return MCLTheme.borderColor[this.classValue as ColorPalette]
365
+ }
366
+ getBorderXColorClass(): string {
367
+ if (!this.validateColorType) {
368
+ return ' '
369
+ }
370
+ return MCLTheme.borderXColor[this.classValue as ColorPalette]
371
+ }
372
+ getBorderYColorClass(): string {
373
+ if (!this.validateColorType) {
374
+ return ' '
375
+ }
376
+ return MCLTheme.borderYColor[this.classValue as ColorPalette]
377
+ }
378
+ getBorderTopColorClass(): string {
379
+ if (!this.validateColorType) {
380
+ return ' '
381
+ }
382
+ return MCLTheme.borderTopColor[this.classValue as ColorPalette]
383
+ }
384
+ getBorderBottomColorClass(): string {
385
+ if (!this.validateColorType) {
386
+ return ' '
387
+ }
388
+ return MCLTheme.borderBottomColor[this.classValue as ColorPalette]
389
+ }
390
+ getBorderLeftColorClass(): string {
391
+ if (!this.validateColorType) {
392
+ return ' '
393
+ }
394
+ return MCLTheme.borderLeftColor[this.classValue as ColorPalette]
395
+ }
396
+ getBorderRightColorClass(): string {
397
+ if (!this.validateColorType) {
398
+ return ' '
399
+ }
400
+ return MCLTheme.borderRightColor[this.classValue as ColorPalette]
401
+ }
402
+ // handle heading classes
403
+ getH1Class(): string {
404
+ if (!this.validateHeadingTextSizeType) {
405
+ return ' '
406
+ }
407
+ return MCLTheme.heading1[this.classValue as HeadingSize]
408
+ }
409
+ getH2Class(): string {
410
+ if (!this.validateHeadingTextSizeType) {
411
+ return ' '
412
+ }
413
+ return MCLTheme.heading2[this.classValue as HeadingSize]
414
+ }
415
+ getH3Class(): string {
416
+ if (!this.validateHeadingTextSizeType) {
417
+ return ' '
418
+ }
419
+ return MCLTheme.heading3[this.classValue as HeadingSize]
420
+ }
421
+ getH4Class(): string {
422
+ if (!this.validateHeadingTextSizeType) {
423
+ return ' '
424
+ }
425
+ return MCLTheme.heading4[this.classValue as HeadingSize]
426
+ }
427
+ // handle body text size classes
428
+ getBodyTextSizeClass(): string {
429
+ if (!this.validateBodyTextSizeType) {
430
+ return ' '
431
+ }
432
+ return MCLTheme.bodyText[this.classValue as BodyText]
433
+ }
434
+ // handle font weight classes
435
+ getFontWeightClass(): string {
436
+ if (!this.validateFontWeightType) {
437
+ return ' '
438
+ }
439
+ return MCLTheme.fontweight[this.classValue as FontWeight]
440
+ }
441
+ // handle text align classes
442
+ getTextAlignClass(): string {
443
+ return MCLTheme.textAlign[this.classValue as Position]
444
+ }
445
+ // handle border width classes
446
+ getBorderWidthClass(): string {
447
+ if (!this.validateRangeType) {
448
+ return ' '
449
+ }
450
+ return MCLTheme.borderWidth[this.classValue as Range<1, 12>]
451
+ }
452
+ getBorderXWidthClass(): string {
453
+ if (!this.validateRangeType) {
454
+ return ' '
455
+ }
456
+ return MCLTheme.borderXWidth[this.classValue as Range<1, 12>]
457
+ }
458
+ getBorderYWidthClass(): string {
459
+ if (!this.validateRangeType) {
460
+ return ' '
461
+ }
462
+ return MCLTheme.borderYWidth[this.classValue as Range<1, 12>]
463
+ }
464
+ getBorderTopWidthClass(): string {
465
+ if (!this.validateRangeType) {
466
+ return ' '
467
+ }
468
+ return MCLTheme.borderTopWidth[this.classValue as Range<1, 12>]
469
+ }
470
+ getBorderBottomWidthClass(): string {
471
+ if (!this.validateRangeType) {
472
+ return ' '
473
+ }
474
+ return MCLTheme.borderBottomWidth[this.classValue as Range<1, 12>]
475
+ }
476
+ getBorderLeftWidthClass(): string {
477
+ if (!this.validateRangeType) {
478
+ return ' '
479
+ }
480
+ return MCLTheme.borderLeftWidth[this.classValue as Range<1, 12>]
481
+ }
482
+ getBorderRightWidthClass(): string {
483
+ if (!this.validateRangeType) {
484
+ return ' '
485
+ }
486
+ return MCLTheme.borderRightWidth[this.classValue as Range<1, 12>]
487
+ }
488
+ // handle opacity classes
489
+ getOpacityClass(): string {
490
+ if (!this.validateOpacityType) {
491
+ return ' '
492
+ }
493
+ return MCLTheme.opacity[this.classValue as OpacityRange]
494
+ }
495
+ getBgOpacityClass(): string {
496
+ if (!this.validateOpacityType) {
497
+ return ' '
498
+ }
499
+ return MCLTheme.backgroundOpacity[this.classValue as OpacityRange]
500
+ }
501
+ // handle spacing classes
502
+ // margins
503
+ getMarginClass(): string {
504
+ if (!this.validateSpaceType) {
505
+ return ' '
506
+ }
507
+ return MCLTheme.margin[this.classValue as SpacingLevel]
508
+ }
509
+ getMarginXClass(): string {
510
+ if (!this.validateSpaceType) {
511
+ return ' '
512
+ }
513
+ return MCLTheme.marginX[this.classValue as SpacingLevel]
514
+ }
515
+ getMarginYClass(): string {
516
+ if (!this.validateSpaceType) {
517
+ return ' '
518
+ }
519
+ return MCLTheme.marginY[this.classValue as SpacingLevel]
520
+ }
521
+ getMarginTopClass(): string {
522
+ if (!this.validateSpaceType) {
523
+ return ' '
524
+ }
525
+ return MCLTheme.marginTop[this.classValue as SpacingLevel]
526
+ }
527
+ getMarginBottomClass(): string {
528
+ if (!this.validateSpaceType) {
529
+ return ' '
530
+ }
531
+ return MCLTheme.marginBottom[this.classValue as SpacingLevel]
532
+ }
533
+ getMarginLeftClass(): string {
534
+ if (!this.validateSpaceType) {
535
+ return ' '
536
+ }
537
+ return MCLTheme.marginLeft[this.classValue as SpacingLevel]
538
+ }
539
+ getMarginRightClass(): string {
540
+ if (!this.validateSpaceType) {
541
+ return ' '
542
+ }
543
+ return MCLTheme.marginRight[this.classValue as SpacingLevel]
544
+ }
545
+ // padding
546
+ getPaddingClass(): string {
547
+ if (!this.validateSpaceType) {
548
+ return ' '
549
+ }
550
+ return MCLTheme.padding[this.classValue as SpacingLevel]
551
+ }
552
+ getPaddingXClass(): string {
553
+ if (!this.validateSpaceType) {
554
+ return ' '
555
+ }
556
+ return MCLTheme.paddingX[this.classValue as SpacingLevel]
557
+ }
558
+ getPaddingYClass(): string {
559
+ if (!this.validateSpaceType) {
560
+ return ' '
561
+ }
562
+ return MCLTheme.paddingY[this.classValue as SpacingLevel]
563
+ }
564
+ getPaddingTopClass(): string {
565
+ if (!this.validateSpaceType) {
566
+ return ' '
567
+ }
568
+ return MCLTheme.paddingTop[this.classValue as SpacingLevel]
569
+ }
570
+ getPaddingBottomClass(): string {
571
+ if (!this.validateSpaceType) {
572
+ return ' '
573
+ }
574
+ return MCLTheme.paddingBottom[this.classValue as SpacingLevel]
575
+ }
576
+ getPaddingLeftClass(): string {
577
+ if (!this.validateSpaceType) {
578
+ return ' '
579
+ }
580
+ return MCLTheme.paddingLeft[this.classValue as SpacingLevel]
581
+ }
582
+ getPaddingRightClass(): string {
583
+ if (!this.validateSpaceType) {
584
+ return ' '
585
+ }
586
+ return MCLTheme.paddingRight[this.classValue as SpacingLevel]
587
+ }
588
+ // gap
589
+ getGapClass(): string {
590
+ if (!this.validateSpaceType) {
591
+ return ' '
592
+ }
593
+ return MCLTheme.gap[this.classValue as SpacingLevel]
594
+ }
595
+ // input validation
596
+ validateColorType(): boolean {
597
+ const validatorRe =
598
+ /(primary|secondary|success|info|warning|danger|light-1|light-2|light-3|light-4|dark-1|dark-2|dark-3|dark-4|black|white|transparent)/
599
+ if (typeof this.classValue !== 'string') {
600
+ return false
601
+ }
602
+ return validatorRe.test(this.classValue)
603
+ }
604
+ validateSpaceType(): boolean {
605
+ const validatorRe = /(0|3xs|2xs|xs|sm|md|lg|xl|2xl|3xl)/
606
+ if (typeof this.classValue !== 'string') {
607
+ return false
608
+ }
609
+ return validatorRe.test(this.classValue)
610
+ }
611
+ validateHeadingTextSizeType(): boolean {
612
+ const validatorRe = /(sm|md|lg|xl)/
613
+ if (typeof this.classValue !== 'string') {
614
+ return false
615
+ }
616
+ return validatorRe.test(this.classValue)
617
+ }
618
+ validateBodyTextSizeType(): boolean {
619
+ const validatorRe = /(xs|sm|md|lg|xl)/
620
+ if (typeof this.classValue !== 'string') {
621
+ return false
622
+ }
623
+ return validatorRe.test(this.classValue)
624
+ }
625
+ validateFontWeightType(): boolean {
626
+ const validatorRe = /(light|normal|bold)/
627
+ if (typeof this.classValue !== 'string') {
628
+ return false
629
+ }
630
+ return validatorRe.test(this.classValue)
631
+ }
632
+ validateOpacityType(): boolean {
633
+ const validatorRe = /\b(0|[1-9]0|100)\b/
634
+ if (typeof this.classValue !== 'number') {
635
+ return false
636
+ }
637
+ return validatorRe.test(this.classValue.toString())
638
+ }
639
+ validateRangeType(): boolean {
640
+ const validatorRe = /\b([1-9]|1[0-2])\b/
641
+ if (typeof this.classValue !== 'number') {
642
+ return false
643
+ }
644
+ return validatorRe.test(this.classValue.toString())
645
+ }
646
+ validateAlignment(): boolean {
647
+ const validatorRe = /(left|center|right)/
648
+ if (typeof this.classValue !== 'string') {
649
+ return false
650
+ }
651
+ return validatorRe.test(this.classValue)
652
+ }
653
+ dummyMethod(): any {
654
+ return this.getRingOffsetColorClass()
655
+ }
656
+ }
657
+
658
+ export default GenerateMCLClass