|
| 1 | +import type { Config } from '@master/css' |
| 2 | +import CSSRuntime from './core' |
| 3 | +import initCSSRuntime from './init' |
| 4 | + |
| 5 | +type CSSRuntimeHostConstructor = new (...args: any[]) => HTMLElement |
| 6 | +type CSSRuntimeConfig = Config | ((host: HTMLElement) => Config | undefined) |
| 7 | +type CSSRuntimeRoot = ShadowRoot | ((host: HTMLElement) => ShadowRoot | null | undefined) |
| 8 | + |
| 9 | +export interface CSSRuntimeDecoratorOptions { |
| 10 | + config?: CSSRuntimeConfig |
| 11 | + root?: CSSRuntimeRoot |
| 12 | + autoObserve?: boolean |
| 13 | +} |
| 14 | + |
| 15 | +function isOptions(value: CSSRuntimeConfig | CSSRuntimeDecoratorOptions | undefined): value is CSSRuntimeDecoratorOptions { |
| 16 | + return !!value && ( |
| 17 | + 'config' in value || |
| 18 | + 'root' in value || |
| 19 | + 'autoObserve' in value |
| 20 | + ) |
| 21 | +} |
| 22 | + |
| 23 | +function resolveConfig(host: HTMLElement, config: CSSRuntimeConfig | undefined): Config | undefined { |
| 24 | + return typeof config === 'function' ? config(host) : config |
| 25 | +} |
| 26 | + |
| 27 | +function resolveRoot(host: HTMLElement, root: CSSRuntimeRoot | undefined): ShadowRoot | null | undefined { |
| 28 | + return typeof root === 'function' |
| 29 | + ? root(host) |
| 30 | + : root || host.shadowRoot |
| 31 | +} |
| 32 | + |
| 33 | +export default function cssRuntime(config?: CSSRuntimeConfig): <T extends CSSRuntimeHostConstructor>(target: T) => T |
| 34 | +export default function cssRuntime(options?: CSSRuntimeDecoratorOptions): <T extends CSSRuntimeHostConstructor>(target: T) => T |
| 35 | +export default function cssRuntime(config?: CSSRuntimeConfig, options?: Omit<CSSRuntimeDecoratorOptions, 'config'>): <T extends CSSRuntimeHostConstructor>(target: T) => T |
| 36 | +export default function cssRuntime( |
| 37 | + configOrOptions?: CSSRuntimeConfig | CSSRuntimeDecoratorOptions, |
| 38 | + options?: Omit<CSSRuntimeDecoratorOptions, 'config'> |
| 39 | +) { |
| 40 | + const resolvedOptions = isOptions(configOrOptions) |
| 41 | + ? configOrOptions |
| 42 | + : { ...options, config: configOrOptions } |
| 43 | + |
| 44 | + return function <T extends CSSRuntimeHostConstructor>(target: T): T { |
| 45 | + const connectedCallback = target.prototype.connectedCallback as (() => void) | undefined |
| 46 | + const disconnectedCallback = target.prototype.disconnectedCallback as (() => void) | undefined |
| 47 | + |
| 48 | + return class extends target { |
| 49 | + cssRuntime?: CSSRuntime |
| 50 | + |
| 51 | + connectedCallback() { |
| 52 | + connectedCallback?.call(this) |
| 53 | + const root = resolveRoot(this, resolvedOptions.root) |
| 54 | + if (!root) { |
| 55 | + throw new Error('`@cssRuntime()` requires a shadow root. Provide `options.root` or create a shadow root before `connectedCallback()` finishes.') |
| 56 | + } |
| 57 | + this.cssRuntime = initCSSRuntime( |
| 58 | + resolveConfig(this, resolvedOptions.config), |
| 59 | + root, |
| 60 | + resolvedOptions.autoObserve |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + disconnectedCallback() { |
| 65 | + disconnectedCallback?.call(this) |
| 66 | + this.cssRuntime?.destroy() |
| 67 | + this.cssRuntime = undefined |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments