@varo-ui/theme 1.2.0 → 2.1.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/src/css.ts CHANGED
@@ -1,23 +1,83 @@
1
1
  import type { ThemeDefinition } from './types'
2
2
 
3
3
  export interface ThemeCssVariables {
4
+ '--varo-ui-white': string
5
+ '--varo-ui-black': string
4
6
  '--varo-ui-bg': string
5
7
  '--varo-ui-surface': string
6
8
  '--varo-ui-surface-muted': string
9
+ '--varo-ui-surface-overlay': string
7
10
  '--varo-ui-text': string
11
+ '--varo-ui-text-regular': string
8
12
  '--varo-ui-text-muted': string
13
+ '--varo-ui-text-placeholder': string
14
+ '--varo-ui-text-disabled': string
9
15
  '--varo-ui-border': string
10
16
  '--varo-ui-border-strong': string
17
+ '--varo-ui-border-dark': string
18
+ '--varo-ui-border-light': string
19
+ '--varo-ui-border-lighter': string
20
+ '--varo-ui-border-extra-light': string
21
+ '--varo-ui-fill': string
22
+ '--varo-ui-fill-darker': string
23
+ '--varo-ui-fill-dark': string
24
+ '--varo-ui-fill-light': string
25
+ '--varo-ui-fill-lighter': string
26
+ '--varo-ui-fill-extra-light': string
11
27
  '--varo-ui-primary': string
28
+ '--varo-ui-primary-dark': string
29
+ '--varo-ui-primary-hover': string
30
+ '--varo-ui-primary-soft': string
12
31
  '--varo-ui-primary-foreground': string
32
+ '--varo-ui-primary-hover-foreground': string
33
+ '--varo-ui-primary-text': string
13
34
  '--varo-ui-success': string
35
+ '--varo-ui-success-dark': string
36
+ '--varo-ui-success-hover': string
37
+ '--varo-ui-success-soft': string
38
+ '--varo-ui-success-foreground': string
39
+ '--varo-ui-success-hover-foreground': string
40
+ '--varo-ui-success-text': string
14
41
  '--varo-ui-warning': string
42
+ '--varo-ui-warning-dark': string
43
+ '--varo-ui-warning-hover': string
44
+ '--varo-ui-warning-soft': string
45
+ '--varo-ui-warning-foreground': string
46
+ '--varo-ui-warning-hover-foreground': string
47
+ '--varo-ui-warning-text': string
15
48
  '--varo-ui-danger': string
49
+ '--varo-ui-danger-dark': string
50
+ '--varo-ui-danger-hover': string
51
+ '--varo-ui-danger-soft': string
52
+ '--varo-ui-danger-foreground': string
53
+ '--varo-ui-danger-hover-foreground': string
54
+ '--varo-ui-danger-text': string
55
+ '--varo-ui-info': string
56
+ '--varo-ui-info-dark': string
57
+ '--varo-ui-info-hover': string
58
+ '--varo-ui-info-soft': string
59
+ '--varo-ui-info-foreground': string
60
+ '--varo-ui-info-hover-foreground': string
61
+ '--varo-ui-info-text': string
16
62
  '--varo-ui-radius': string
17
63
  '--varo-ui-radius-sm': string
64
+ '--varo-ui-button-radius': string
65
+ '--varo-ui-button-height-sm': string
66
+ '--varo-ui-button-height-md': string
67
+ '--varo-ui-button-height-lg': string
68
+ '--varo-ui-input-radius': string
69
+ '--varo-ui-input-height-sm': string
70
+ '--varo-ui-input-height-md': string
71
+ '--varo-ui-input-height-lg': string
18
72
  '--varo-ui-shadow-sm': string
19
73
  '--varo-ui-shadow-md': string
74
+ '--varo-ui-motion-feedback': string
75
+ '--varo-ui-motion-state': string
76
+ '--varo-ui-motion-enter': string
77
+ '--varo-ui-motion-exit': string
78
+ '--varo-ui-motion-ease': string
20
79
  '--varo-ui-ring': string
80
+ '--varo-ui-focus': string
21
81
  [name: `--${string}`]: string
22
82
  }
23
83
 
@@ -39,28 +99,165 @@ function colorWithAlpha(color: string, alpha: number): string {
39
99
  return `rgb(${red} ${green} ${blue} / ${alpha}%)`
40
100
  }
41
101
 
102
+ function semanticTextColor(base: string, text: string): string {
103
+ if (!/^#[\da-f]{6}$/i.test(base) || !/^#[\da-f]{6}$/i.test(text)) {
104
+ return text
105
+ }
106
+
107
+ const channels = [1, 3, 5].map((offset) => {
108
+ const baseChannel = Number.parseInt(base.slice(offset, offset + 2), 16)
109
+ const textChannel = Number.parseInt(text.slice(offset, offset + 2), 16)
110
+ return Math.round((baseChannel + textChannel) / 2)
111
+ .toString(16)
112
+ .padStart(2, '0')
113
+ })
114
+
115
+ return `#${channels.join('')}`
116
+ }
117
+
118
+ function relativeLuminance(color: string): number | undefined {
119
+ const value = color.slice(1)
120
+ const normalized = value.length === 3
121
+ ? value.split('').map(part => `${part}${part}`).join('')
122
+ : value
123
+
124
+ if (!color.startsWith('#') || !/^[\da-f]{6}$/i.test(normalized)) {
125
+ return undefined
126
+ }
127
+
128
+ const channelLuminance = (offset: number) => {
129
+ const channel = Number.parseInt(normalized.slice(offset, offset + 2), 16) / 255
130
+ return channel <= 0.04045
131
+ ? channel / 12.92
132
+ : ((channel + 0.055) / 1.055) ** 2.4
133
+ }
134
+ return channelLuminance(0) * 0.2126
135
+ + channelLuminance(2) * 0.7152
136
+ + channelLuminance(4) * 0.0722
137
+ }
138
+
139
+ function isChromatic(color: string): boolean {
140
+ const value = color.slice(1)
141
+ const normalized = value.length === 3
142
+ ? value.split('').map(part => `${part}${part}`).join('')
143
+ : value
144
+
145
+ if (!color.startsWith('#') || !/^[\da-f]{6}$/i.test(normalized)) {
146
+ return false
147
+ }
148
+
149
+ const red = Number.parseInt(normalized.slice(0, 2), 16)
150
+ const green = Number.parseInt(normalized.slice(2, 4), 16)
151
+ const blue = Number.parseInt(normalized.slice(4, 6), 16)
152
+ return Math.max(red, green, blue) - Math.min(red, green, blue) >= 40
153
+ }
154
+
155
+ export function contrastSafeForeground(background: string): '#000000' | '#ffffff' {
156
+ const backgroundLuminance = relativeLuminance(background)
157
+ if (backgroundLuminance == null) {
158
+ throw new Error(`Contrast-safe foreground requires a hex color (#RGB or #RRGGBB): ${background}`)
159
+ }
160
+
161
+ const blackContrast = (backgroundLuminance + 0.05) / 0.05
162
+ const whiteContrast = 1.05 / (backgroundLuminance + 0.05)
163
+
164
+ if (backgroundLuminance >= 0.55) {
165
+ return '#000000'
166
+ }
167
+
168
+ if (isChromatic(background)) {
169
+ return '#ffffff'
170
+ }
171
+
172
+ return blackContrast >= whiteContrast ? '#000000' : '#ffffff'
173
+ }
174
+
42
175
  export function createThemeCssVariables(
43
176
  theme: ThemeDefinition,
44
177
  overrides: ThemeCssVariableOverrides = {},
45
178
  ): ThemeCssVariables {
46
179
  return {
180
+ '--varo-ui-white': theme.palette.neutral.white,
181
+ '--varo-ui-black': theme.palette.neutral.black,
47
182
  '--varo-ui-bg': theme.semantic.surfaceElevated,
48
183
  '--varo-ui-surface': theme.semantic.surfaceBase,
49
- '--varo-ui-surface-muted': theme.palette.neutral['50'],
184
+ '--varo-ui-surface-muted': theme.semantic.fillLight,
185
+ '--varo-ui-surface-overlay': theme.semantic.surfaceOverlay,
50
186
  '--varo-ui-text': theme.semantic.textBase,
187
+ '--varo-ui-text-regular': theme.semantic.textRegular,
51
188
  '--varo-ui-text-muted': theme.semantic.textMuted,
189
+ '--varo-ui-text-placeholder': theme.semantic.textPlaceholder,
190
+ '--varo-ui-text-disabled': theme.semantic.textDisabled,
52
191
  '--varo-ui-border': theme.semantic.borderBase,
53
192
  '--varo-ui-border-strong': theme.semantic.borderStrong,
193
+ '--varo-ui-border-dark': theme.palette.neutral['border-dark'],
194
+ '--varo-ui-border-light': theme.semantic.borderLight,
195
+ '--varo-ui-border-lighter': theme.semantic.borderLighter,
196
+ '--varo-ui-border-extra-light': theme.palette.neutral['border-extra-light'],
197
+ '--varo-ui-fill': theme.semantic.fillBase,
198
+ '--varo-ui-fill-darker': theme.palette.neutral['fill-darker'],
199
+ '--varo-ui-fill-dark': theme.palette.neutral['fill-dark'],
200
+ '--varo-ui-fill-light': theme.semantic.fillLight,
201
+ '--varo-ui-fill-lighter': theme.semantic.fillLighter,
202
+ '--varo-ui-fill-extra-light': theme.palette.neutral['fill-extra-light'],
54
203
  '--varo-ui-primary': theme.semantic.primaryBase,
55
- '--varo-ui-primary-foreground': theme.palette.neutral['0'],
56
- '--varo-ui-success': theme.seed.success,
57
- '--varo-ui-warning': theme.seed.warning,
204
+ '--varo-ui-primary-dark': theme.semantic.primaryDark,
205
+ '--varo-ui-primary-hover': theme.semantic.primaryHover,
206
+ '--varo-ui-primary-soft': theme.semantic.primarySoft,
207
+ '--varo-ui-primary-foreground': contrastSafeForeground(theme.semantic.primaryBase),
208
+ '--varo-ui-primary-hover-foreground': contrastSafeForeground(theme.semantic.primaryHover),
209
+ '--varo-ui-primary-text': semanticTextColor(theme.semantic.primaryBase, theme.semantic.textBase),
210
+ '--varo-ui-success': theme.semantic.successBase,
211
+ '--varo-ui-success-dark': theme.semantic.successDark,
212
+ '--varo-ui-success-hover': theme.semantic.successHover,
213
+ '--varo-ui-success-soft': theme.semantic.successSoft,
214
+ '--varo-ui-success-foreground': contrastSafeForeground(theme.semantic.successBase),
215
+ '--varo-ui-success-hover-foreground': contrastSafeForeground(theme.semantic.successHover),
216
+ '--varo-ui-success-text': semanticTextColor(theme.semantic.successBase, theme.semantic.textBase),
217
+ '--varo-ui-warning': theme.semantic.warningBase,
218
+ '--varo-ui-warning-dark': theme.semantic.warningDark,
219
+ '--varo-ui-warning-hover': theme.semantic.warningHover,
220
+ '--varo-ui-warning-soft': theme.semantic.warningSoft,
221
+ '--varo-ui-warning-foreground': contrastSafeForeground(theme.semantic.warningBase),
222
+ '--varo-ui-warning-hover-foreground': contrastSafeForeground(theme.semantic.warningHover),
223
+ '--varo-ui-warning-text': semanticTextColor(theme.semantic.warningBase, theme.semantic.textBase),
58
224
  '--varo-ui-danger': theme.semantic.dangerBase,
59
- '--varo-ui-radius': theme.components.button.borderRadius,
60
- '--varo-ui-radius-sm': theme.components.input.borderRadius,
61
- '--varo-ui-shadow-sm': '0 1px 2px rgb(24 36 51 / 6%)',
62
- '--varo-ui-shadow-md': '0 14px 34px rgb(24 36 51 / 12%)',
225
+ '--varo-ui-danger-dark': theme.semantic.dangerDark,
226
+ '--varo-ui-danger-hover': theme.semantic.dangerHover,
227
+ '--varo-ui-danger-soft': theme.semantic.dangerSoft,
228
+ '--varo-ui-danger-foreground': contrastSafeForeground(theme.semantic.dangerBase),
229
+ '--varo-ui-danger-hover-foreground': contrastSafeForeground(theme.semantic.dangerHover),
230
+ '--varo-ui-danger-text': semanticTextColor(theme.semantic.dangerBase, theme.semantic.textBase),
231
+ '--varo-ui-info': theme.semantic.infoBase,
232
+ '--varo-ui-info-dark': theme.semantic.infoDark,
233
+ '--varo-ui-info-hover': theme.semantic.infoHover,
234
+ '--varo-ui-info-soft': theme.semantic.infoSoft,
235
+ '--varo-ui-info-foreground': contrastSafeForeground(theme.semantic.infoBase),
236
+ '--varo-ui-info-hover-foreground': contrastSafeForeground(theme.semantic.infoHover),
237
+ '--varo-ui-info-text': semanticTextColor(theme.semantic.infoBase, theme.semantic.textBase),
238
+ '--varo-ui-button-radius': theme.components.button.borderRadius,
239
+ '--varo-ui-button-height-sm': theme.components.button.heightSm,
240
+ '--varo-ui-button-height-md': theme.components.button.heightMd,
241
+ '--varo-ui-button-height-lg': theme.components.button.heightLg,
242
+ '--varo-ui-input-radius': theme.components.input.borderRadius,
243
+ '--varo-ui-input-height-sm': theme.components.input.heightSm,
244
+ '--varo-ui-input-height-md': theme.components.input.heightMd,
245
+ '--varo-ui-input-height-lg': theme.components.input.heightLg,
246
+ '--varo-ui-radius': 'var(--varo-ui-button-radius)',
247
+ '--varo-ui-radius-sm': 'var(--varo-ui-input-radius)',
248
+ '--varo-ui-shadow-sm': theme.seed.mode === 'dark'
249
+ ? '0 1px 2px rgb(0 0 0 / 46%)'
250
+ : '0 1px 2px rgb(48 49 51 / 6%)',
251
+ '--varo-ui-shadow-md': theme.seed.mode === 'dark'
252
+ ? '0 12px 32px 4px rgb(0 0 0 / 36%), 0 8px 20px rgb(0 0 0 / 72%)'
253
+ : '0 12px 32px 4px rgb(0 0 0 / 4%), 0 8px 20px rgb(0 0 0 / 8%)',
254
+ '--varo-ui-motion-feedback': '140ms',
255
+ '--varo-ui-motion-state': '180ms',
256
+ '--varo-ui-motion-enter': '220ms',
257
+ '--varo-ui-motion-exit': '160ms',
258
+ '--varo-ui-motion-ease': 'cubic-bezier(0.2, 0.8, 0.2, 1)',
63
259
  '--varo-ui-ring': colorWithAlpha(theme.semantic.primaryBase, 16),
260
+ '--varo-ui-focus': contrastSafeForeground(theme.semantic.surfaceBase),
64
261
  ...overrides,
65
262
  }
66
263
  }
package/src/provider.ts CHANGED
@@ -1,36 +1,168 @@
1
- import type { App, InjectionKey, Plugin } from 'vue'
2
- import type { ThemeDefinition } from './types'
1
+ import type {
2
+ App,
3
+ ComputedRef,
4
+ InjectionKey,
5
+ Plugin,
6
+ PropType,
7
+ } from 'vue'
8
+ import type { ThemeCssVariables } from './css'
9
+ import type { ThemeDefinition, ThemeOverrides } from './types'
3
10
  import type { ThemeConfig } from './types-runtime'
4
- import { computed, inject, provide } from 'vue'
5
- import { createTheme } from './theme'
11
+ import { computed, defineComponent, h, inject, provide, toValue, watchEffect } from 'vue'
12
+ import { createThemeCssVariables } from './css'
13
+ import { createTheme, mergeThemeOverrides } from './theme'
6
14
 
7
- const ThemeKey: InjectionKey<ThemeConfig> = Symbol('varo-theme')
15
+ const ThemeKey: InjectionKey<ComputedRef<ThemeDefinition>> = Symbol('varo-theme')
16
+ const fallbackTheme = createTheme({
17
+ primary: '#07c160',
18
+ success: '#13b248',
19
+ warning: '#fa9200',
20
+ error: '#eb3437',
21
+ neutral: '#303133',
22
+ info: '#73767a',
23
+ })
8
24
 
9
- export function provideVaroTheme(config: ThemeConfig) {
10
- provide(ThemeKey, config)
25
+ interface InlineStyleSnapshot {
26
+ priority: string
27
+ value: string
11
28
  }
12
29
 
13
- export function useVaroTheme() {
14
- const config = inject(ThemeKey, undefined)
30
+ interface ThemeLayer {
31
+ owner: symbol
32
+ variables: ThemeCssVariables
33
+ }
15
34
 
16
- if (!config) {
17
- return computed<ThemeDefinition>(() =>
18
- createTheme({
19
- primary: '#2563eb',
20
- success: '#16a34a',
21
- warning: '#d97706',
22
- error: '#dc2626',
23
- neutral: '#0f172a',
24
- }),
25
- )
35
+ interface ThemeTargetState {
36
+ base: Map<string, InlineStyleSnapshot>
37
+ layers: ThemeLayer[]
38
+ }
39
+
40
+ const targetStates = new WeakMap<HTMLElement, ThemeTargetState>()
41
+
42
+ function resolveTheme(config: ThemeConfig): ThemeDefinition {
43
+ return mergeThemeOverrides(toValue(config.theme), toValue(config.overrides) ?? {})
44
+ }
45
+
46
+ function applyTargetState(target: HTMLElement, state: ThemeTargetState): void {
47
+ const activeLayer = state.layers[state.layers.length - 1]
48
+ const activeVariables = activeLayer?.variables as unknown as Record<string, string> | undefined
49
+ for (const [name, snapshot] of state.base) {
50
+ const value = activeVariables?.[name]
51
+ if (value !== undefined) {
52
+ target.style.setProperty(name, value)
53
+ }
54
+ else if (snapshot.value) {
55
+ target.style.setProperty(name, snapshot.value, snapshot.priority)
56
+ }
57
+ else {
58
+ target.style.removeProperty(name)
59
+ }
26
60
  }
61
+ }
62
+
63
+ function bindThemeVariables(
64
+ target: HTMLElement,
65
+ theme: ComputedRef<ThemeDefinition>,
66
+ ): () => void {
67
+ let state = targetStates.get(target)
68
+ if (!state) {
69
+ state = { base: new Map(), layers: [] }
70
+ targetStates.set(target, state)
71
+ }
72
+
73
+ const owner = Symbol('varo-theme-owner')
74
+ const layer: ThemeLayer = {
75
+ owner,
76
+ variables: createThemeCssVariables(theme.value),
77
+ }
78
+ state.layers.push(layer)
79
+
80
+ const stop = watchEffect(() => {
81
+ const variables = createThemeCssVariables(theme.value)
82
+ for (const name of Object.keys(variables)) {
83
+ if (!state!.base.has(name)) {
84
+ state!.base.set(name, {
85
+ priority: target.style.getPropertyPriority(name),
86
+ value: target.style.getPropertyValue(name),
87
+ })
88
+ }
89
+ }
90
+ layer.variables = variables
91
+ if (state!.layers[state!.layers.length - 1]?.owner === owner) {
92
+ applyTargetState(target, state!)
93
+ }
94
+ })
27
95
 
28
- return computed(() => config.theme)
96
+ return () => {
97
+ stop()
98
+ const index = state!.layers.findIndex(candidate => candidate.owner === owner)
99
+ if (index < 0) {
100
+ return
101
+ }
102
+ const wasActive = index === state!.layers.length - 1
103
+ state!.layers.splice(index, 1)
104
+ if (wasActive) {
105
+ applyTargetState(target, state!)
106
+ }
107
+ if (state!.layers.length === 0) {
108
+ targetStates.delete(target)
109
+ }
110
+ }
111
+ }
112
+
113
+ export function provideVaroTheme(config: ThemeConfig): void {
114
+ provide(ThemeKey, computed(() => resolveTheme(config)))
29
115
  }
30
116
 
117
+ export function useVaroTheme(): ComputedRef<ThemeDefinition> {
118
+ return inject(ThemeKey, computed(() => fallbackTheme))
119
+ }
120
+
121
+ export const VaroThemeProvider = defineComponent({
122
+ name: 'VaroThemeProvider',
123
+ inheritAttrs: false,
124
+ props: {
125
+ theme: {
126
+ type: Object as PropType<ThemeDefinition>,
127
+ required: true,
128
+ },
129
+ overrides: {
130
+ type: Object as PropType<ThemeOverrides>,
131
+ default: undefined,
132
+ },
133
+ },
134
+ setup(props, { attrs, slots }) {
135
+ const theme = computed(() =>
136
+ mergeThemeOverrides(props.theme, props.overrides ?? {}),
137
+ )
138
+ const styles = computed(() => createThemeCssVariables(theme.value))
139
+ provide(ThemeKey, theme)
140
+
141
+ return () =>
142
+ h(
143
+ 'div',
144
+ {
145
+ ...attrs,
146
+ style: [styles.value, attrs.style],
147
+ },
148
+ slots.default?.(),
149
+ )
150
+ },
151
+ })
152
+
31
153
  export const VaroConfigProvider = {
32
154
  install(app: App, config?: ThemeConfig) {
33
- if (!config) { throw new Error('VaroConfigProvider requires a theme configuration.') }
34
- app.provide(ThemeKey, config)
155
+ if (!config) {
156
+ throw new Error('VaroConfigProvider requires a theme configuration.')
157
+ }
158
+
159
+ const theme = computed(() => resolveTheme(config))
160
+ app.provide(ThemeKey, theme)
161
+ if (typeof document === 'undefined') {
162
+ return
163
+ }
164
+
165
+ const release = bindThemeVariables(config.target ?? document.documentElement, theme)
166
+ app.onUnmount(release)
35
167
  },
36
168
  } satisfies Plugin