|
| 1 | +import plugin from "tailwindcss/plugin"; |
| 2 | +import type { IProvider } from "../../declaration/provider.interface"; |
| 3 | +import type { IWindowSizing } from "../../declaration/window-sizing.interface"; |
| 4 | +import { Strings } from "../../utils/strings"; |
| 5 | +import { Validates } from "../../utils/validates"; |
| 6 | + |
| 7 | +export type TSizingProviderConstructorParams = { |
| 8 | + |
| 9 | + /** |
| 10 | + * @description |
| 11 | + * tokens' prefix. |
| 12 | + * |
| 13 | + * @default 'md-sys-sizing' |
| 14 | + * |
| 15 | + * @example If the value of prefix is 'your-prefix'. |
| 16 | + * ```css |
| 17 | + * .w-compact { |
| 18 | + * width: var(--your-prefix-width-compact, 600px); |
| 19 | + * } |
| 20 | + * ``` |
| 21 | + */ |
| 22 | + readonly prefix: string |
| 23 | + |
| 24 | + /** |
| 25 | + * @description |
| 26 | + * When set to true, each token contains a default value. |
| 27 | + * |
| 28 | + * @default true |
| 29 | + * |
| 30 | + * @example |
| 31 | + * ```typescript |
| 32 | + * const color = provideSizing({ |
| 33 | + * // .w-compact { width: var(--md-sys-sizing-width-compact, 600px); } |
| 34 | + * hardcodeDefaultValue: true, |
| 35 | + * |
| 36 | + * // .w-compact { width: var(--md-sys-sizing-width-compact, ); } |
| 37 | + * hardcodeDefaultValue: false, |
| 38 | + * }) |
| 39 | + * ``` |
| 40 | + */ |
| 41 | + readonly hardcodeDefaultValue: boolean |
| 42 | + |
| 43 | + /** |
| 44 | + * @description |
| 45 | + * This option can customize the CSS value corresponding to certain tokens. |
| 46 | + * Note that this option ignores [hardcodeDefaultValue] and [prefix]. |
| 47 | + * |
| 48 | + * @default [] |
| 49 | + * |
| 50 | + * @example |
| 51 | + * ```typescript |
| 52 | + * const color = provideSizing({ |
| 53 | + * customTokens: { |
| 54 | + * // .w-compact { var(--md-sys-sizing-width-compact, 100px); } |
| 55 | + * 'compact': '100px' |
| 56 | + * }, |
| 57 | + * }) |
| 58 | + * ``` |
| 59 | + */ |
| 60 | + readonly customTokens: Omit<IWindowSizing, 'extraLarge'> | Record<string, string> |
| 61 | + |
| 62 | + /** |
| 63 | + * @description |
| 64 | + * Exclude some unnecessary tokens. |
| 65 | + * |
| 66 | + * @default [] |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ```typescript |
| 70 | + * const color = provideSizing({ |
| 71 | + * excludedTokens: [ |
| 72 | + * // remove .w-compact and .max-w-screen-compact |
| 73 | + * 'compact', |
| 74 | + * ], |
| 75 | + * }) |
| 76 | + * ``` |
| 77 | + */ |
| 78 | + readonly excludedTokens: Array<keyof (Omit<IWindowSizing, 'extraLarge'>) | {}> |
| 79 | +} |
| 80 | + |
| 81 | +abstract class DefaultWindowSizing implements IWindowSizing { |
| 82 | + compact = '600px' |
| 83 | + medium = '840px' |
| 84 | + expanded = '1200px' |
| 85 | + large = '1600px' |
| 86 | + |
| 87 | + /** |
| 88 | + * extraLarge: width >= 1600px |
| 89 | + * @deprecated |
| 90 | + */ |
| 91 | + extraLarge = '1600px' |
| 92 | +} |
| 93 | + |
| 94 | +class DefaultSizingTokens extends DefaultWindowSizing { |
| 95 | + public get defaultTokensRecord() { |
| 96 | + return { |
| 97 | + compact: this.compact, |
| 98 | + medium: this.medium, |
| 99 | + expanded: this.expanded, |
| 100 | + large: this.large |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export class SizingProvider extends DefaultSizingTokens implements IProvider { |
| 106 | + public prefix |
| 107 | + public tokens |
| 108 | + public hardcodeDefaultValue |
| 109 | + public excludedTokens |
| 110 | + public customTokens |
| 111 | + |
| 112 | + constructor(params: Partial<TSizingProviderConstructorParams>) { |
| 113 | + super() |
| 114 | + this.prefix = params.prefix ?? 'md-sys-sizing' |
| 115 | + this.hardcodeDefaultValue = params.hardcodeDefaultValue ?? true |
| 116 | + this.excludedTokens = params.excludedTokens ?? [] |
| 117 | + this.customTokens = params.customTokens ?? {} |
| 118 | + this.tokens = this.validate([this.customTokens, this.defaultTokensRecord, this.excludedTokens]) |
| 119 | + } |
| 120 | + |
| 121 | + private validate(params: Parameters<typeof Validates.validate>) { |
| 122 | + return Validates.validate(...params) |
| 123 | + } |
| 124 | + |
| 125 | + protected transformTokensToCssRuleObject(prefix: string, tokens: Record<string, string>, hardcodeDefaultValue: boolean) { |
| 126 | + const cssPropertyComputed = (name: string, value: string) => !Object.hasOwn(this.customTokens, name) ? `var(--${prefix}-width-${Strings.toKebabCase(name)}, ${hardcodeDefaultValue ? value : ''})` : `${value}` |
| 127 | + |
| 128 | + const width = Validates.transformTokenRecordToCssRuleObject( |
| 129 | + tokens, |
| 130 | + (name) => `.w-${Strings.toKebabCase(name)}`, |
| 131 | + (name, value) => ({ |
| 132 | + 'width': cssPropertyComputed(name, value) |
| 133 | + }) |
| 134 | + ) |
| 135 | + const maxWidthScreen = Validates.transformTokenRecordToCssRuleObject( |
| 136 | + tokens, |
| 137 | + (name) => `.max-w-screen-${Strings.toKebabCase(name)}`, |
| 138 | + (name, value) => ({ |
| 139 | + 'max-width': cssPropertyComputed(name, value) |
| 140 | + }) |
| 141 | + ) |
| 142 | + return Object.assign({}, width, maxWidthScreen) |
| 143 | + } |
| 144 | + |
| 145 | + getPlugin() { |
| 146 | + const tokens = this.transformTokensToCssRuleObject(this.prefix, this.tokens, this.hardcodeDefaultValue) |
| 147 | + console.log(tokens); |
| 148 | + |
| 149 | + return plugin(({ addUtilities }) => { |
| 150 | + addUtilities(tokens) |
| 151 | + }) |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments