@umber-style/vue 0.0.0 → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +11 -1
- package/dist/index.cjs +213 -0
- package/dist/index.d.cts +98 -0
- package/dist/index.d.mts +98 -0
- package/dist/index.mjs +157 -0
- package/package.json +68 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 soroush.tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
# @umber-style/vue
|
|
2
|
+
|
|
3
|
+
The Vue binding of UmberStyle: the same names as the React binding, with a reactive `useTheme`.
|
|
4
|
+
|
|
5
|
+
Install: `pnpm add @umber-style/vue`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { styled, ThemeProvider } from '@umber-style/vue'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Documentation: [docs](../../docs/README.md), and [Vue](../../docs/vue.md) for this package.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
let vue = require("vue");
|
|
4
|
+
// src/styled.ts
|
|
5
|
+
/** Whether `tag` is a component made by `styled`. */
|
|
6
|
+
function isStyledComponent(tag) {
|
|
7
|
+
return typeof tag === "function" && "_styles" in tag;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Makes `styled` over an engine: a functional component that renders the tag with one class. A
|
|
11
|
+
* function style gets the view of the theme of the nearest provider, which `useThemeView` reads.
|
|
12
|
+
*/
|
|
13
|
+
function createStyled(engine, useThemeView) {
|
|
14
|
+
return (tag, options = {}) => (...styles) => {
|
|
15
|
+
const inner = isStyledComponent(tag) ? tag : void 0;
|
|
16
|
+
const baseTag = inner?._tag ?? tag;
|
|
17
|
+
const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
|
|
18
|
+
const styler = engine.createStyler(options, styles, inner?._styles);
|
|
19
|
+
const Styled = Object.assign((_props, { attrs, slots }) => {
|
|
20
|
+
const className = styler.getClassName(attrs, [...(0, _umber_style_core.flattenStyle)(attrs.style, engine.cache.findEntry), (0, vue.normalizeClass)(attrs.class)], styler.isDynamic ? useThemeView() : void 0);
|
|
21
|
+
const element = attrs.as ?? baseTag;
|
|
22
|
+
const filter = typeof element === "string" ? shouldForward : void 0;
|
|
23
|
+
const forwarded = {};
|
|
24
|
+
for (const [key, value] of Object.entries(attrs)) if (!(key === "as" || key === "theme" || key === "class" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
|
|
25
|
+
forwarded.class = className || void 0;
|
|
26
|
+
return (0, vue.h)(element, forwarded, slots);
|
|
27
|
+
}, {
|
|
28
|
+
_styles: styler.styles,
|
|
29
|
+
_tag: baseTag,
|
|
30
|
+
_shouldForwardProp: shouldForward
|
|
31
|
+
});
|
|
32
|
+
Styled.inheritAttrs = false;
|
|
33
|
+
return Styled;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// src/ThemeProvider.ts
|
|
37
|
+
/** Makes the `ThemeProvider` of an engine: an element with the theme class, which provides the real values below it. */
|
|
38
|
+
function createThemeProvider(engine, key) {
|
|
39
|
+
return (0, vue.defineComponent)({
|
|
40
|
+
name: "ThemeProvider",
|
|
41
|
+
props: {
|
|
42
|
+
theme: {
|
|
43
|
+
type: Object,
|
|
44
|
+
required: true
|
|
45
|
+
},
|
|
46
|
+
colorScheme: {
|
|
47
|
+
type: String,
|
|
48
|
+
default: void 0
|
|
49
|
+
},
|
|
50
|
+
as: {
|
|
51
|
+
type: String,
|
|
52
|
+
default: "div"
|
|
53
|
+
},
|
|
54
|
+
/** Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
|
|
55
|
+
isRoot: {
|
|
56
|
+
type: Boolean,
|
|
57
|
+
default: false
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
setup(props, { slots }) {
|
|
61
|
+
const outer = (0, vue.inject)(key, null);
|
|
62
|
+
(0, vue.provide)(key, (0, vue.computed)(() => {
|
|
63
|
+
const base = outer?.value ?? engine.baseTheme;
|
|
64
|
+
const theme = props.theme;
|
|
65
|
+
return base === void 0 ? theme : (0, _umber_style_core.mergeDeep)(base, theme);
|
|
66
|
+
}));
|
|
67
|
+
return () => {
|
|
68
|
+
const theme = props.theme;
|
|
69
|
+
if (props.isRoot) {
|
|
70
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
71
|
+
return (0, vue.h)(vue.Fragment, null, slots.default?.());
|
|
72
|
+
}
|
|
73
|
+
const scope = engine.themeClass(theme, props.colorScheme);
|
|
74
|
+
return (0, vue.h)(props.as, { class: [scope, engine.scopeClass()] }, slots.default?.());
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
// src/createStyle.ts
|
|
80
|
+
/** Makes the Vue styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
|
|
81
|
+
function bindEngine(engine) {
|
|
82
|
+
const key = Symbol("theme");
|
|
83
|
+
const useTheme = () => (0, vue.inject)(key, null) ?? (0, vue.computed)(() => {
|
|
84
|
+
const { baseTheme } = engine;
|
|
85
|
+
if (baseTheme === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
|
|
86
|
+
return baseTheme;
|
|
87
|
+
});
|
|
88
|
+
const useThemeView = () => engine.createThemeView(useTheme().value);
|
|
89
|
+
function create(styles) {
|
|
90
|
+
return engine.createSheet(styles);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
engine,
|
|
94
|
+
cache: engine.cache,
|
|
95
|
+
styled: createStyled(engine, useThemeView),
|
|
96
|
+
StyleSheet: { create },
|
|
97
|
+
useStyle: ((sheet) => (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.resolve(useThemeView()) : sheet),
|
|
98
|
+
apply: (...entries) => ({ class: engine.css(...entries) || void 0 }),
|
|
99
|
+
ThemeProvider: createThemeProvider(engine, key),
|
|
100
|
+
useTheme,
|
|
101
|
+
themeClass: engine.themeClass,
|
|
102
|
+
when: engine.when,
|
|
103
|
+
whenScheme: engine.whenScheme
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Makes the Vue styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
107
|
+
function createStyle(theme, options = {}) {
|
|
108
|
+
const engine = options.engine ?? (0, _umber_style_core.createEngine)(theme, options);
|
|
109
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
110
|
+
return bindEngine(engine);
|
|
111
|
+
}
|
|
112
|
+
// src/defaultStyle.ts
|
|
113
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
114
|
+
function createDefaultStyle() {
|
|
115
|
+
let pending = {};
|
|
116
|
+
let bound;
|
|
117
|
+
const getStyle = () => {
|
|
118
|
+
if (!bound) {
|
|
119
|
+
const engine = pending.engine ?? (0, _umber_style_core.createLazyEngine)(pending);
|
|
120
|
+
if (pending.theme) engine.setTheme(pending.theme);
|
|
121
|
+
bound = bindEngine(engine);
|
|
122
|
+
}
|
|
123
|
+
return bound;
|
|
124
|
+
};
|
|
125
|
+
/** Sets the options of the default engine, and gives it a base theme or an engine to share. Throws once the default style was used. */
|
|
126
|
+
function configure(options) {
|
|
127
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
128
|
+
pending = options;
|
|
129
|
+
}
|
|
130
|
+
const styled = (tag, options) => getStyle().styled(tag, options);
|
|
131
|
+
const StyleSheet = { create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : (0, _umber_style_core.createDeferredRecord)(styles, () => getStyle().StyleSheet.create(styles))) };
|
|
132
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
133
|
+
function useTheme() {
|
|
134
|
+
return getStyle().useTheme();
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
configure,
|
|
138
|
+
getEngine: () => getStyle().engine,
|
|
139
|
+
styled,
|
|
140
|
+
StyleSheet,
|
|
141
|
+
useStyle,
|
|
142
|
+
useTheme,
|
|
143
|
+
ThemeProvider: (0, vue.defineComponent)({
|
|
144
|
+
name: "ThemeProvider",
|
|
145
|
+
inheritAttrs: false,
|
|
146
|
+
setup(_props, { attrs, slots }) {
|
|
147
|
+
const Provider = getStyle().ThemeProvider;
|
|
148
|
+
return () => (0, vue.h)(Provider, attrs, slots);
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
152
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
153
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
154
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const { configure, getEngine, styled, StyleSheet, useStyle, useTheme, ThemeProvider, apply, themeClass, when, whenScheme } = createDefaultStyle();
|
|
158
|
+
exports.StyleSheet = StyleSheet;
|
|
159
|
+
exports.ThemeProvider = ThemeProvider;
|
|
160
|
+
Object.defineProperty(exports, "add", {
|
|
161
|
+
enumerable: true,
|
|
162
|
+
get: function() {
|
|
163
|
+
return _umber_style_core.add;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
Object.defineProperty(exports, "alpha", {
|
|
167
|
+
enumerable: true,
|
|
168
|
+
get: function() {
|
|
169
|
+
return _umber_style_core.alpha;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
exports.apply = apply;
|
|
173
|
+
Object.defineProperty(exports, "at", {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
get: function() {
|
|
176
|
+
return _umber_style_core.at;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
exports.bindEngine = bindEngine;
|
|
180
|
+
exports.configure = configure;
|
|
181
|
+
exports.createDefaultStyle = createDefaultStyle;
|
|
182
|
+
exports.createStyle = createStyle;
|
|
183
|
+
Object.defineProperty(exports, "divide", {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
get: function() {
|
|
186
|
+
return _umber_style_core.divide;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
exports.getEngine = getEngine;
|
|
190
|
+
Object.defineProperty(exports, "multiply", {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function() {
|
|
193
|
+
return _umber_style_core.multiply;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
Object.defineProperty(exports, "negate", {
|
|
197
|
+
enumerable: true,
|
|
198
|
+
get: function() {
|
|
199
|
+
return _umber_style_core.negate;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
exports.styled = styled;
|
|
203
|
+
Object.defineProperty(exports, "subtract", {
|
|
204
|
+
enumerable: true,
|
|
205
|
+
get: function() {
|
|
206
|
+
return _umber_style_core.subtract;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
exports.themeClass = themeClass;
|
|
210
|
+
exports.useStyle = useStyle;
|
|
211
|
+
exports.useTheme = useTheme;
|
|
212
|
+
exports.when = when;
|
|
213
|
+
exports.whenScheme = whenScheme;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Component, ComputedRef, FunctionalComponent } from "vue";
|
|
2
|
+
import { AlphaColor, BindingOptions, ColorScheme, DeepPartial, Engine, NestedKey, ObjectSheet, SchemeKey, SchemeName, SheetEntry, SheetEntry as SheetEntry$1, SheetStyles, StyleInput, StyleObject, StylerOptions, Theme, ThemeKey, WebNested, WebProperties, WebStyleObject, WebStyleObject as WebStyleObject$1, add, alpha, at, divide, multiply, negate, subtract } from "@umber-style/core";
|
|
3
|
+
export type StyleOptions<T = never> = BindingOptions<T>;
|
|
4
|
+
export interface StyledOptions extends StylerOptions {
|
|
5
|
+
/** Whether an attribute is forwarded to a string tag. Default: every attribute but `as`, `theme`, `class` and `style`, which the component handles itself. */
|
|
6
|
+
shouldForwardProp?: (prop: string) => boolean;
|
|
7
|
+
}
|
|
8
|
+
/** What a styled component keeps, so that styling it again flattens into one element. */
|
|
9
|
+
export interface StyledStatics {
|
|
10
|
+
/** Every style of the component, the styles of the components it wraps first. */
|
|
11
|
+
_styles: readonly StyleInput[];
|
|
12
|
+
/** The tag or component that is finally rendered. */
|
|
13
|
+
_tag: string | Component;
|
|
14
|
+
_shouldForwardProp?: (prop: string) => boolean;
|
|
15
|
+
}
|
|
16
|
+
export type StyledComponent = FunctionalComponent<Record<string, unknown>> & StyledStatics;
|
|
17
|
+
export type Styled<T> = (tag: string | Component, options?: StyledOptions) => (...styles: StyleInput<T, Record<string, unknown>>[]) => StyledComponent;
|
|
18
|
+
/** A set of named styles made from a function of the theme: it has no entries until `useStyle` resolves it. */
|
|
19
|
+
export interface Sheet<S extends SheetStyles> {
|
|
20
|
+
resolve(theme?: unknown): { [K in keyof S]: SheetEntry$1; };
|
|
21
|
+
}
|
|
22
|
+
export interface ThemeProviderProps<T> {
|
|
23
|
+
/** The theme, or the part of it that this subtree changes. */
|
|
24
|
+
theme: DeepPartial<T>;
|
|
25
|
+
colorScheme?: ColorScheme;
|
|
26
|
+
/** The element that carries the theme class. Default 'div'. */
|
|
27
|
+
as?: string;
|
|
28
|
+
/** Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
|
|
29
|
+
isRoot?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface Style<T> {
|
|
32
|
+
engine: Engine<T>;
|
|
33
|
+
cache: Engine<T>['cache'];
|
|
34
|
+
styled: Styled<T>;
|
|
35
|
+
StyleSheet: {
|
|
36
|
+
/** A sheet given as an object is its entries, usable at once, with no hook and no theme. */
|
|
37
|
+
create<K extends string>(styles: ObjectSheet<K>): { readonly [P in K]: SheetEntry$1; };
|
|
38
|
+
/** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
|
|
39
|
+
create<K extends string>(styles: (theme: T) => Record<K, WebStyleObject$1>): Sheet<Record<K, WebStyleObject$1>>;
|
|
40
|
+
};
|
|
41
|
+
/** The entries of a sheet; an object sheet is its own entries and is returned as it is. */
|
|
42
|
+
useStyle<K extends string>(sheet: { readonly [P in K]: SheetEntry$1; }): { readonly [P in K]: SheetEntry$1; };
|
|
43
|
+
useStyle<S extends SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: SheetEntry$1; };
|
|
44
|
+
/** The attributes that apply entries of a sheet: a class. */
|
|
45
|
+
apply(...entries: (SheetEntry$1 | string | null | undefined | false)[]): {
|
|
46
|
+
class?: string;
|
|
47
|
+
};
|
|
48
|
+
ThemeProvider: Component<ThemeProviderProps<T>>;
|
|
49
|
+
/** The real theme values, merged over the providers above; reactive. Call it in `setup`. */
|
|
50
|
+
useTheme(): ComputedRef<T>;
|
|
51
|
+
themeClass: Engine<T>['themeClass'];
|
|
52
|
+
/** A style key for "the nearest theme is this theme"; it needs `@scope`. */
|
|
53
|
+
when: Engine<T>['when'];
|
|
54
|
+
/** A style key for "the nearest color scheme is this one": the nearest theme given that `colorScheme`, else the system preference. */
|
|
55
|
+
whenScheme: Engine<T>['whenScheme'];
|
|
56
|
+
}
|
|
57
|
+
export interface DefaultStyleOptions extends BindingOptions<Theme> {
|
|
58
|
+
/** The base theme, for an app that has no provider at all. */
|
|
59
|
+
theme?: Theme;
|
|
60
|
+
}
|
|
61
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
62
|
+
export declare function createDefaultStyle(): {
|
|
63
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
64
|
+
getEngine: () => import("@umber-style/core").Engine<Theme>;
|
|
65
|
+
styled: Styled<Theme>;
|
|
66
|
+
StyleSheet: {
|
|
67
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
68
|
+
create<K extends string>(styles: (theme: Theme) => Record<K, WebStyleObject$1>): Sheet<Record<K, WebStyleObject$1>>;
|
|
69
|
+
};
|
|
70
|
+
useStyle: {
|
|
71
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
72
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
73
|
+
};
|
|
74
|
+
useTheme: () => ComputedRef<Theme>;
|
|
75
|
+
ThemeProvider: import("vue").Component<ThemeProviderProps<Theme>>;
|
|
76
|
+
apply: Style<Theme>['apply'];
|
|
77
|
+
when: Style<Theme>['when'];
|
|
78
|
+
whenScheme: Style<Theme>['whenScheme'];
|
|
79
|
+
themeClass: Style<Theme>['themeClass'];
|
|
80
|
+
};
|
|
81
|
+
/** Makes the Vue styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
|
|
82
|
+
export declare function bindEngine<T extends object>(engine: Engine<T>): Style<T>;
|
|
83
|
+
/** Makes the Vue styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
84
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): Style<T>;
|
|
85
|
+
export declare const configure: (options: DefaultStyleOptions) => void, getEngine: () => import("@umber-style/core").Engine<import("@umber-style/core").Theme>, styled: Styled<import("@umber-style/core").Theme>, StyleSheet: {
|
|
86
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
87
|
+
create<K extends string>(styles: (theme: import("@umber-style/core").Theme) => Record<K, import("@umber-style/core").WebStyleObject>): Sheet<Record<K, import("@umber-style/core").WebStyleObject>>;
|
|
88
|
+
}, useStyle: {
|
|
89
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
90
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
91
|
+
}, useTheme: () => import("vue").ComputedRef<import("@umber-style/core").Theme>, ThemeProvider: import("vue").Component<ThemeProviderProps<import("@umber-style/core").Theme>>, apply: (...entries: (import("@umber-style/core").SheetEntry | string | null | undefined | false)[]) => {
|
|
92
|
+
class?: string;
|
|
93
|
+
}, themeClass: (theme: {
|
|
94
|
+
readonly _theme?: never;
|
|
95
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => string, when: (theme: {
|
|
96
|
+
readonly _theme?: never;
|
|
97
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => import("@umber-style/core").ThemeKey, whenScheme: (scheme: import("@umber-style/core").SchemeName) => import("@umber-style/core").SchemeKey;
|
|
98
|
+
export { type AlphaColor, type NestedKey, type SchemeKey, type SchemeName, type SheetEntry, type StyleObject, type ThemeKey, type WebNested, type WebProperties, type WebStyleObject, add, alpha, at, divide, multiply, negate, subtract };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Component, ComputedRef, FunctionalComponent } from "vue";
|
|
2
|
+
import { AlphaColor, BindingOptions, ColorScheme, DeepPartial, Engine, NestedKey, ObjectSheet, SchemeKey, SchemeName, SheetEntry, SheetEntry as SheetEntry$1, SheetStyles, StyleInput, StyleObject, StylerOptions, Theme, ThemeKey, WebNested, WebProperties, WebStyleObject, WebStyleObject as WebStyleObject$1, add, alpha, at, divide, multiply, negate, subtract } from "@umber-style/core";
|
|
3
|
+
export type StyleOptions<T = never> = BindingOptions<T>;
|
|
4
|
+
export interface StyledOptions extends StylerOptions {
|
|
5
|
+
/** Whether an attribute is forwarded to a string tag. Default: every attribute but `as`, `theme`, `class` and `style`, which the component handles itself. */
|
|
6
|
+
shouldForwardProp?: (prop: string) => boolean;
|
|
7
|
+
}
|
|
8
|
+
/** What a styled component keeps, so that styling it again flattens into one element. */
|
|
9
|
+
export interface StyledStatics {
|
|
10
|
+
/** Every style of the component, the styles of the components it wraps first. */
|
|
11
|
+
_styles: readonly StyleInput[];
|
|
12
|
+
/** The tag or component that is finally rendered. */
|
|
13
|
+
_tag: string | Component;
|
|
14
|
+
_shouldForwardProp?: (prop: string) => boolean;
|
|
15
|
+
}
|
|
16
|
+
export type StyledComponent = FunctionalComponent<Record<string, unknown>> & StyledStatics;
|
|
17
|
+
export type Styled<T> = (tag: string | Component, options?: StyledOptions) => (...styles: StyleInput<T, Record<string, unknown>>[]) => StyledComponent;
|
|
18
|
+
/** A set of named styles made from a function of the theme: it has no entries until `useStyle` resolves it. */
|
|
19
|
+
export interface Sheet<S extends SheetStyles> {
|
|
20
|
+
resolve(theme?: unknown): { [K in keyof S]: SheetEntry$1; };
|
|
21
|
+
}
|
|
22
|
+
export interface ThemeProviderProps<T> {
|
|
23
|
+
/** The theme, or the part of it that this subtree changes. */
|
|
24
|
+
theme: DeepPartial<T>;
|
|
25
|
+
colorScheme?: ColorScheme;
|
|
26
|
+
/** The element that carries the theme class. Default 'div'. */
|
|
27
|
+
as?: string;
|
|
28
|
+
/** Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
|
|
29
|
+
isRoot?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface Style<T> {
|
|
32
|
+
engine: Engine<T>;
|
|
33
|
+
cache: Engine<T>['cache'];
|
|
34
|
+
styled: Styled<T>;
|
|
35
|
+
StyleSheet: {
|
|
36
|
+
/** A sheet given as an object is its entries, usable at once, with no hook and no theme. */
|
|
37
|
+
create<K extends string>(styles: ObjectSheet<K>): { readonly [P in K]: SheetEntry$1; };
|
|
38
|
+
/** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
|
|
39
|
+
create<K extends string>(styles: (theme: T) => Record<K, WebStyleObject$1>): Sheet<Record<K, WebStyleObject$1>>;
|
|
40
|
+
};
|
|
41
|
+
/** The entries of a sheet; an object sheet is its own entries and is returned as it is. */
|
|
42
|
+
useStyle<K extends string>(sheet: { readonly [P in K]: SheetEntry$1; }): { readonly [P in K]: SheetEntry$1; };
|
|
43
|
+
useStyle<S extends SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: SheetEntry$1; };
|
|
44
|
+
/** The attributes that apply entries of a sheet: a class. */
|
|
45
|
+
apply(...entries: (SheetEntry$1 | string | null | undefined | false)[]): {
|
|
46
|
+
class?: string;
|
|
47
|
+
};
|
|
48
|
+
ThemeProvider: Component<ThemeProviderProps<T>>;
|
|
49
|
+
/** The real theme values, merged over the providers above; reactive. Call it in `setup`. */
|
|
50
|
+
useTheme(): ComputedRef<T>;
|
|
51
|
+
themeClass: Engine<T>['themeClass'];
|
|
52
|
+
/** A style key for "the nearest theme is this theme"; it needs `@scope`. */
|
|
53
|
+
when: Engine<T>['when'];
|
|
54
|
+
/** A style key for "the nearest color scheme is this one": the nearest theme given that `colorScheme`, else the system preference. */
|
|
55
|
+
whenScheme: Engine<T>['whenScheme'];
|
|
56
|
+
}
|
|
57
|
+
export interface DefaultStyleOptions extends BindingOptions<Theme> {
|
|
58
|
+
/** The base theme, for an app that has no provider at all. */
|
|
59
|
+
theme?: Theme;
|
|
60
|
+
}
|
|
61
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
62
|
+
export declare function createDefaultStyle(): {
|
|
63
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
64
|
+
getEngine: () => import("@umber-style/core").Engine<Theme>;
|
|
65
|
+
styled: Styled<Theme>;
|
|
66
|
+
StyleSheet: {
|
|
67
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
68
|
+
create<K extends string>(styles: (theme: Theme) => Record<K, WebStyleObject$1>): Sheet<Record<K, WebStyleObject$1>>;
|
|
69
|
+
};
|
|
70
|
+
useStyle: {
|
|
71
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
72
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
73
|
+
};
|
|
74
|
+
useTheme: () => ComputedRef<Theme>;
|
|
75
|
+
ThemeProvider: import("vue").Component<ThemeProviderProps<Theme>>;
|
|
76
|
+
apply: Style<Theme>['apply'];
|
|
77
|
+
when: Style<Theme>['when'];
|
|
78
|
+
whenScheme: Style<Theme>['whenScheme'];
|
|
79
|
+
themeClass: Style<Theme>['themeClass'];
|
|
80
|
+
};
|
|
81
|
+
/** Makes the Vue styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
|
|
82
|
+
export declare function bindEngine<T extends object>(engine: Engine<T>): Style<T>;
|
|
83
|
+
/** Makes the Vue styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
84
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): Style<T>;
|
|
85
|
+
export declare const configure: (options: DefaultStyleOptions) => void, getEngine: () => import("@umber-style/core").Engine<import("@umber-style/core").Theme>, styled: Styled<import("@umber-style/core").Theme>, StyleSheet: {
|
|
86
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
87
|
+
create<K extends string>(styles: (theme: import("@umber-style/core").Theme) => Record<K, import("@umber-style/core").WebStyleObject>): Sheet<Record<K, import("@umber-style/core").WebStyleObject>>;
|
|
88
|
+
}, useStyle: {
|
|
89
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
90
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: Sheet<S>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
91
|
+
}, useTheme: () => import("vue").ComputedRef<import("@umber-style/core").Theme>, ThemeProvider: import("vue").Component<ThemeProviderProps<import("@umber-style/core").Theme>>, apply: (...entries: (import("@umber-style/core").SheetEntry | string | null | undefined | false)[]) => {
|
|
92
|
+
class?: string;
|
|
93
|
+
}, themeClass: (theme: {
|
|
94
|
+
readonly _theme?: never;
|
|
95
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => string, when: (theme: {
|
|
96
|
+
readonly _theme?: never;
|
|
97
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => import("@umber-style/core").ThemeKey, whenScheme: (scheme: import("@umber-style/core").SchemeName) => import("@umber-style/core").SchemeKey;
|
|
98
|
+
export { type AlphaColor, type NestedKey, type SchemeKey, type SchemeName, type SheetEntry, type StyleObject, type ThemeKey, type WebNested, type WebProperties, type WebStyleObject, add, alpha, at, divide, multiply, negate, subtract };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { NO_THEME_MESSAGE, add, alpha, at, createDeferredRecord, createEngine, createLazyEngine, divide, flattenStyle, isThemedSheet, mergeDeep, multiply, negate, subtract } from "@umber-style/core";
|
|
2
|
+
import { Fragment, computed, defineComponent, h, inject, normalizeClass, provide } from "vue";
|
|
3
|
+
// src/styled.ts
|
|
4
|
+
/** Whether `tag` is a component made by `styled`. */
|
|
5
|
+
function isStyledComponent(tag) {
|
|
6
|
+
return typeof tag === "function" && "_styles" in tag;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Makes `styled` over an engine: a functional component that renders the tag with one class. A
|
|
10
|
+
* function style gets the view of the theme of the nearest provider, which `useThemeView` reads.
|
|
11
|
+
*/
|
|
12
|
+
function createStyled(engine, useThemeView) {
|
|
13
|
+
return (tag, options = {}) => (...styles) => {
|
|
14
|
+
const inner = isStyledComponent(tag) ? tag : void 0;
|
|
15
|
+
const baseTag = inner?._tag ?? tag;
|
|
16
|
+
const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
|
|
17
|
+
const styler = engine.createStyler(options, styles, inner?._styles);
|
|
18
|
+
const Styled = Object.assign((_props, { attrs, slots }) => {
|
|
19
|
+
const className = styler.getClassName(attrs, [...flattenStyle(attrs.style, engine.cache.findEntry), normalizeClass(attrs.class)], styler.isDynamic ? useThemeView() : void 0);
|
|
20
|
+
const element = attrs.as ?? baseTag;
|
|
21
|
+
const filter = typeof element === "string" ? shouldForward : void 0;
|
|
22
|
+
const forwarded = {};
|
|
23
|
+
for (const [key, value] of Object.entries(attrs)) if (!(key === "as" || key === "theme" || key === "class" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
|
|
24
|
+
forwarded.class = className || void 0;
|
|
25
|
+
return h(element, forwarded, slots);
|
|
26
|
+
}, {
|
|
27
|
+
_styles: styler.styles,
|
|
28
|
+
_tag: baseTag,
|
|
29
|
+
_shouldForwardProp: shouldForward
|
|
30
|
+
});
|
|
31
|
+
Styled.inheritAttrs = false;
|
|
32
|
+
return Styled;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// src/ThemeProvider.ts
|
|
36
|
+
/** Makes the `ThemeProvider` of an engine: an element with the theme class, which provides the real values below it. */
|
|
37
|
+
function createThemeProvider(engine, key) {
|
|
38
|
+
return defineComponent({
|
|
39
|
+
name: "ThemeProvider",
|
|
40
|
+
props: {
|
|
41
|
+
theme: {
|
|
42
|
+
type: Object,
|
|
43
|
+
required: true
|
|
44
|
+
},
|
|
45
|
+
colorScheme: {
|
|
46
|
+
type: String,
|
|
47
|
+
default: void 0
|
|
48
|
+
},
|
|
49
|
+
as: {
|
|
50
|
+
type: String,
|
|
51
|
+
default: "div"
|
|
52
|
+
},
|
|
53
|
+
/** Renders no element and no class: the app puts `themeClass(theme)` on an ancestor, usually `<html>`. */
|
|
54
|
+
isRoot: {
|
|
55
|
+
type: Boolean,
|
|
56
|
+
default: false
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
setup(props, { slots }) {
|
|
60
|
+
const outer = inject(key, null);
|
|
61
|
+
provide(key, computed(() => {
|
|
62
|
+
const base = outer?.value ?? engine.baseTheme;
|
|
63
|
+
const theme = props.theme;
|
|
64
|
+
return base === void 0 ? theme : mergeDeep(base, theme);
|
|
65
|
+
}));
|
|
66
|
+
return () => {
|
|
67
|
+
const theme = props.theme;
|
|
68
|
+
if (props.isRoot) {
|
|
69
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
70
|
+
return h(Fragment, null, slots.default?.());
|
|
71
|
+
}
|
|
72
|
+
const scope = engine.themeClass(theme, props.colorScheme);
|
|
73
|
+
return h(props.as, { class: [scope, engine.scopeClass()] }, slots.default?.());
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// src/createStyle.ts
|
|
79
|
+
/** Makes the Vue styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
|
|
80
|
+
function bindEngine(engine) {
|
|
81
|
+
const key = Symbol("theme");
|
|
82
|
+
const useTheme = () => inject(key, null) ?? computed(() => {
|
|
83
|
+
const { baseTheme } = engine;
|
|
84
|
+
if (baseTheme === void 0) throw new Error(NO_THEME_MESSAGE);
|
|
85
|
+
return baseTheme;
|
|
86
|
+
});
|
|
87
|
+
const useThemeView = () => engine.createThemeView(useTheme().value);
|
|
88
|
+
function create(styles) {
|
|
89
|
+
return engine.createSheet(styles);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
engine,
|
|
93
|
+
cache: engine.cache,
|
|
94
|
+
styled: createStyled(engine, useThemeView),
|
|
95
|
+
StyleSheet: { create },
|
|
96
|
+
useStyle: ((sheet) => isThemedSheet(sheet) ? sheet.resolve(useThemeView()) : sheet),
|
|
97
|
+
apply: (...entries) => ({ class: engine.css(...entries) || void 0 }),
|
|
98
|
+
ThemeProvider: createThemeProvider(engine, key),
|
|
99
|
+
useTheme,
|
|
100
|
+
themeClass: engine.themeClass,
|
|
101
|
+
when: engine.when,
|
|
102
|
+
whenScheme: engine.whenScheme
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Makes the Vue styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
106
|
+
function createStyle(theme, options = {}) {
|
|
107
|
+
const engine = options.engine ?? createEngine(theme, options);
|
|
108
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
109
|
+
return bindEngine(engine);
|
|
110
|
+
}
|
|
111
|
+
// src/defaultStyle.ts
|
|
112
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
113
|
+
function createDefaultStyle() {
|
|
114
|
+
let pending = {};
|
|
115
|
+
let bound;
|
|
116
|
+
const getStyle = () => {
|
|
117
|
+
if (!bound) {
|
|
118
|
+
const engine = pending.engine ?? createLazyEngine(pending);
|
|
119
|
+
if (pending.theme) engine.setTheme(pending.theme);
|
|
120
|
+
bound = bindEngine(engine);
|
|
121
|
+
}
|
|
122
|
+
return bound;
|
|
123
|
+
};
|
|
124
|
+
/** Sets the options of the default engine, and gives it a base theme or an engine to share. Throws once the default style was used. */
|
|
125
|
+
function configure(options) {
|
|
126
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
127
|
+
pending = options;
|
|
128
|
+
}
|
|
129
|
+
const styled = (tag, options) => getStyle().styled(tag, options);
|
|
130
|
+
const StyleSheet = { create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : createDeferredRecord(styles, () => getStyle().StyleSheet.create(styles))) };
|
|
131
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
132
|
+
function useTheme() {
|
|
133
|
+
return getStyle().useTheme();
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
configure,
|
|
137
|
+
getEngine: () => getStyle().engine,
|
|
138
|
+
styled,
|
|
139
|
+
StyleSheet,
|
|
140
|
+
useStyle,
|
|
141
|
+
useTheme,
|
|
142
|
+
ThemeProvider: defineComponent({
|
|
143
|
+
name: "ThemeProvider",
|
|
144
|
+
inheritAttrs: false,
|
|
145
|
+
setup(_props, { attrs, slots }) {
|
|
146
|
+
const Provider = getStyle().ThemeProvider;
|
|
147
|
+
return () => h(Provider, attrs, slots);
|
|
148
|
+
}
|
|
149
|
+
}),
|
|
150
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
151
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
152
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
153
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const { configure, getEngine, styled, StyleSheet, useStyle, useTheme, ThemeProvider, apply, themeClass, when, whenScheme } = createDefaultStyle();
|
|
157
|
+
export { StyleSheet, ThemeProvider, add, alpha, apply, at, bindEngine, configure, createDefaultStyle, createStyle, divide, getEngine, multiply, negate, styled, subtract, themeClass, useStyle, useTheme, when, whenScheme };
|
package/package.json
CHANGED
|
@@ -1,10 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umber-style/vue",
|
|
3
|
-
"version": "0.0.0",
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "UmberStyle for Vue: the same names as the React binding, with a reactive useTheme.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"styling",
|
|
7
|
+
"theme",
|
|
8
|
+
"design-tokens",
|
|
9
|
+
"css-variables",
|
|
10
|
+
"vue",
|
|
11
|
+
"ssr",
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
5
14
|
"license": "MIT",
|
|
15
|
+
"author": "Masoud Soroush <masoud@soroush.tech>",
|
|
16
|
+
"homepage": "https://umber-style.soroush.design",
|
|
6
17
|
"repository": {
|
|
7
18
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/soroush-tech/umber-style.git"
|
|
9
|
-
|
|
10
|
-
}
|
|
19
|
+
"url": "git+https://github.com/soroush-tech/umber-style.git",
|
|
20
|
+
"directory": "packages/vue"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/soroush-tech/umber-style/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/index.d.mts",
|
|
34
|
+
"default": "./dist/index.mjs"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/index.d.cts",
|
|
38
|
+
"default": "./dist/index.cjs"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@umber-style/core": "0.1.0-alpha.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"vue": "^3.5.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^26.6.6",
|
|
53
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
54
|
+
"jsdom": "^31.0.0",
|
|
55
|
+
"oxlint": "^1.93.0",
|
|
56
|
+
"tsdown": "^0.23.7",
|
|
57
|
+
"typescript": "~7.0.2",
|
|
58
|
+
"vitest": "^5.1.0",
|
|
59
|
+
"vue": "^3.5.45",
|
|
60
|
+
"@umber-bench/shared": "0.1.0",
|
|
61
|
+
"@umber-style/eslint-config": "0.0.0"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsdown",
|
|
65
|
+
"lint": "oxlint --deny-warnings",
|
|
66
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.defaultStyle.json",
|
|
67
|
+
"test": "vitest run",
|
|
68
|
+
"test:coverage": "vitest run --coverage"
|
|
69
|
+
},
|
|
70
|
+
"main": "./dist/index.cjs",
|
|
71
|
+
"module": "./dist/index.mjs",
|
|
72
|
+
"types": "./dist/index.d.mts"
|
|
73
|
+
}
|