|
| 1 | +import type { Directive } from 'vue' |
| 2 | + |
| 3 | +abstract class ColorableElement { |
| 4 | + |
| 5 | + /** |
| 6 | + * |
| 7 | + * @param property 'text' | 'background' |
| 8 | + * @param value hex-like |
| 9 | + */ |
| 10 | + protected setElementColorProperty(element: HTMLElement, property: 'text' | 'background', value: string) { |
| 11 | + element.style.setProperty(property === 'text' ? 'color' : 'background', value) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class TextColorDirective extends ColorableElement { |
| 16 | + |
| 17 | + private readonly defaultColor = 'var(--gu-sys-color-on-surface)' |
| 18 | + |
| 19 | + public readonly directive: Directive<HTMLElement, string> = { |
| 20 | + mounted: (el, binding) => { |
| 21 | + const colorRaw = binding.value |
| 22 | + let color = binding.value |
| 23 | + if (typeof colorRaw === 'undefined' || colorRaw === null) { |
| 24 | + console.warn(`v-text-color accepts a string of hexadecimal color values. v-text-color received an unexpected value [${color}].`) |
| 25 | + color = this.defaultColor |
| 26 | + } |
| 27 | + this.setElementColorProperty(el, 'text', color) |
| 28 | + }, |
| 29 | + updated: (el, binding) => { |
| 30 | + if (binding.oldValue === binding.value) return |
| 31 | + const colorRaw = binding.value |
| 32 | + let color = binding.value |
| 33 | + if (typeof colorRaw === 'undefined' || colorRaw === null) { |
| 34 | + console.warn(`v-text-color accepts a string of hexadecimal color values. v-text-color received an unexpected value [${color}].`) |
| 35 | + color = this.defaultColor |
| 36 | + } |
| 37 | + this.setElementColorProperty(el, 'text', color) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +class BackgroundColorDirective extends ColorableElement { |
| 44 | + |
| 45 | + private readonly defaultColor = 'var(--gu-sys-color-surface)' |
| 46 | + |
| 47 | + public readonly directive: Directive<HTMLElement, string> = { |
| 48 | + mounted: (el, binding) => { |
| 49 | + const colorRaw = binding.value |
| 50 | + let color = binding.value |
| 51 | + if (typeof colorRaw === 'undefined' || colorRaw === null) { |
| 52 | + console.warn(`v-text-color accepts a string of hexadecimal color values. v-text-color received an unexpected value [${color}].`) |
| 53 | + color = this.defaultColor |
| 54 | + } |
| 55 | + this.setElementColorProperty(el, 'background', color) |
| 56 | + }, |
| 57 | + updated: (el, binding) => { |
| 58 | + if (binding.oldValue === binding.value) return |
| 59 | + const colorRaw = binding.value |
| 60 | + let color = binding.value |
| 61 | + if (typeof colorRaw === 'undefined' || colorRaw === null) { |
| 62 | + console.warn(`v-text-color accepts a string of hexadecimal color values. v-text-color received an unexpected value [${color}].`) |
| 63 | + color = this.defaultColor |
| 64 | + } |
| 65 | + this.setElementColorProperty(el, 'background', color) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +export const vTextColor = new TextColorDirective().directive |
| 74 | +export const vBackgroundColor = new BackgroundColorDirective().directive |
0 commit comments