svseeds 0.0.6 → 0.0.7
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/README.md +6 -2
- package/_svseeds/core.js +1 -172
- package/_svseeds/core.ts +4 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SvSeeds - Simple Components of Svelte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/svseeds)
|
|
4
|
+
[](https://jsr.io/@svseeds/ui)
|
|
5
|
+
[](LICENSE.md)
|
|
6
|
+
|
|
7
|
+
Simple headless components for Svelte.
|
|
4
8
|
|
|
5
9
|
**WIP**
|
package/_svseeds/core.js
CHANGED
|
@@ -1,172 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { CONST, STATE, AREA, elemId, theme, getClassFn, isUndef, omit, debounce, throttle, };
|
|
3
|
-
const CONST = "constant";
|
|
4
|
-
const STATE = Object.freeze({ DEFAULT: "default", ACTIVE: "active", INACTIVE: "inactive" });
|
|
5
|
-
const AREA = Object.freeze({
|
|
6
|
-
WHOLE: "whole",
|
|
7
|
-
MIDDLE: "middle",
|
|
8
|
-
MAIN: "main",
|
|
9
|
-
TOP: "top",
|
|
10
|
-
LEFT: "left",
|
|
11
|
-
RIGHT: "right",
|
|
12
|
-
BOTTOM: "bottom",
|
|
13
|
-
LABEL: "label",
|
|
14
|
-
AUX: "aux",
|
|
15
|
-
EXTRA: "extra",
|
|
16
|
-
});
|
|
17
|
-
class RandomId {
|
|
18
|
-
static #ALPHABETIC = [...Array.from(Array(25).keys(), (x) => x + 65), ...Array.from(Array(25).keys(), (x) => x + 97)];
|
|
19
|
-
#store = new Set();
|
|
20
|
-
get(v) {
|
|
21
|
-
if (!v)
|
|
22
|
-
return;
|
|
23
|
-
if (this.#store.size > 10000)
|
|
24
|
-
this.#store.clear();
|
|
25
|
-
return this.#add();
|
|
26
|
-
}
|
|
27
|
-
#char() {
|
|
28
|
-
return RandomId.#ALPHABETIC[Math.trunc(Math.random() * RandomId.#ALPHABETIC.length)];
|
|
29
|
-
}
|
|
30
|
-
#gen() {
|
|
31
|
-
return String.fromCharCode(...Array(4).fill(null).map(() => this.#char()), 58);
|
|
32
|
-
}
|
|
33
|
-
#add() {
|
|
34
|
-
let id = this.#gen();
|
|
35
|
-
while (this.#store.has(id)) {
|
|
36
|
-
id = this.#gen();
|
|
37
|
-
}
|
|
38
|
-
this.#store.add(id);
|
|
39
|
-
return id;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
class ThemeSwitcher {
|
|
43
|
-
static #DARK = "dark";
|
|
44
|
-
static #LIGHT = "light";
|
|
45
|
-
#styles = {};
|
|
46
|
-
#current;
|
|
47
|
-
get current() {
|
|
48
|
-
return this.#current;
|
|
49
|
-
}
|
|
50
|
-
constructor() {
|
|
51
|
-
this.#current = ThemeSwitcher.#setInitialTheme();
|
|
52
|
-
}
|
|
53
|
-
setPreset(preset) {
|
|
54
|
-
this.#styles = ThemeSwitcher.#toCSSVarName(preset);
|
|
55
|
-
this.#setColorScheme();
|
|
56
|
-
return this;
|
|
57
|
-
}
|
|
58
|
-
toLight() {
|
|
59
|
-
this.switch(ThemeSwitcher.#LIGHT);
|
|
60
|
-
}
|
|
61
|
-
toDark() {
|
|
62
|
-
this.switch(ThemeSwitcher.#DARK);
|
|
63
|
-
}
|
|
64
|
-
switch(theme) {
|
|
65
|
-
if (!this.#exists(theme))
|
|
66
|
-
return;
|
|
67
|
-
this.#current = theme;
|
|
68
|
-
this.#apply();
|
|
69
|
-
}
|
|
70
|
-
#apply() {
|
|
71
|
-
if (!window)
|
|
72
|
-
return;
|
|
73
|
-
const style = window.document.body.style;
|
|
74
|
-
Object.entries(this.#styles[this.#current])
|
|
75
|
-
.forEach(([name, value]) => style.setProperty(name, value));
|
|
76
|
-
}
|
|
77
|
-
#exists(theme) {
|
|
78
|
-
return Object.keys(this.#styles).includes(theme);
|
|
79
|
-
}
|
|
80
|
-
#setColorScheme() {
|
|
81
|
-
if (!window)
|
|
82
|
-
return;
|
|
83
|
-
const themes = Object.keys(this.#styles).filter((x) => x === ThemeSwitcher.#LIGHT || x === ThemeSwitcher.#DARK);
|
|
84
|
-
window.document.documentElement.style.colorScheme = themes.join(" ");
|
|
85
|
-
}
|
|
86
|
-
static #toCSSVarName(styles) {
|
|
87
|
-
return Object.fromEntries(Object.entries(styles)
|
|
88
|
-
.map(([theme, obj]) => [theme, ThemeSwitcher.#renameProperties(obj)]));
|
|
89
|
-
}
|
|
90
|
-
static #renameProperties(obj) {
|
|
91
|
-
return Object.fromEntries(Object.entries(obj)
|
|
92
|
-
.map(([name, value]) => [`--${name.replaceAll("_", "-")}`, value]));
|
|
93
|
-
}
|
|
94
|
-
static #setInitialTheme() {
|
|
95
|
-
return window?.matchMedia("(prefers-color-scheme: light)").matches ? ThemeSwitcher.#LIGHT : ThemeSwitcher.#DARK;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const elemId = new RandomId();
|
|
99
|
-
const theme = new ThemeSwitcher();
|
|
100
|
-
function getClassFn(name, preset, style) {
|
|
101
|
-
const rule = getRule(name, preset, style);
|
|
102
|
-
if (typeof rule === "string") {
|
|
103
|
-
return (area, status) => cssClass(rule, area, status);
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
return (area, status) => ruleClass(rule, area, status);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
function getRule(name, preset, style) {
|
|
110
|
-
if (typeof style === "string")
|
|
111
|
-
return style.trim() ? style : name;
|
|
112
|
-
const rule = mergeRule(preset, style);
|
|
113
|
-
return Object.keys(rule).length <= 0 ? name : rule;
|
|
114
|
-
}
|
|
115
|
-
function cssClass(name, area, status) {
|
|
116
|
-
return `${name} ${area}${status === STATE.DEFAULT ? "" : ` ${status}`}`;
|
|
117
|
-
}
|
|
118
|
-
function ruleClass(rule, area, status) {
|
|
119
|
-
const constant = rule[area]?.constant ?? "";
|
|
120
|
-
const dynamic = rule[area]?.[status] ?? rule[area]?.default ?? "";
|
|
121
|
-
return constant === "" && dynamic === "" ? undefined : `${constant}${constant && dynamic ? " " : ""}${dynamic}`;
|
|
122
|
-
}
|
|
123
|
-
function mergeRule(preset, style) {
|
|
124
|
-
const presetKeys = Object.keys(preset);
|
|
125
|
-
if (presetKeys.length <= 0)
|
|
126
|
-
return style;
|
|
127
|
-
const styleKeys = Object.keys(style);
|
|
128
|
-
if (styleKeys.length <= 0)
|
|
129
|
-
return preset;
|
|
130
|
-
const result = {};
|
|
131
|
-
new Set([...presetKeys, ...styleKeys]).forEach((key) => {
|
|
132
|
-
result[key] = { ...preset[key] ?? {}, ...style[key] ?? {} };
|
|
133
|
-
});
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
function isUndef(v) {
|
|
137
|
-
return v === void 0;
|
|
138
|
-
}
|
|
139
|
-
function omit(obj, ...keys) {
|
|
140
|
-
if (Object.isFrozen(obj) || Object.isSealed(obj))
|
|
141
|
-
return obj;
|
|
142
|
-
keys.forEach((key) => delete obj[key]);
|
|
143
|
-
return obj;
|
|
144
|
-
}
|
|
145
|
-
function debounce(delay, fn) {
|
|
146
|
-
let timer;
|
|
147
|
-
return (...args) => {
|
|
148
|
-
if (timer)
|
|
149
|
-
clearTimeout(timer);
|
|
150
|
-
timer = setTimeout(() => {
|
|
151
|
-
fn.call(null, ...args);
|
|
152
|
-
}, delay);
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function throttle(interval, fn) {
|
|
156
|
-
let timer;
|
|
157
|
-
let last = 0;
|
|
158
|
-
const elapsed = () => Date.now() - last;
|
|
159
|
-
const run = (args) => {
|
|
160
|
-
fn.call(null, ...args);
|
|
161
|
-
last = Date.now();
|
|
162
|
-
};
|
|
163
|
-
return (...args) => {
|
|
164
|
-
if (!last)
|
|
165
|
-
return run(args);
|
|
166
|
-
clearTimeout(timer);
|
|
167
|
-
timer = setTimeout(() => {
|
|
168
|
-
if (elapsed() >= interval)
|
|
169
|
-
run(args);
|
|
170
|
-
}, interval - elapsed());
|
|
171
|
-
};
|
|
172
|
-
}
|
|
1
|
+
var __assign=this&&this.__assign||function(){__assign=Object.assign||function(e){for(var t,r=1,i=arguments.length;r<i;r++){t=arguments[r];for(var a in t)if(Object.prototype.hasOwnProperty.call(t,a))e[a]=t[a]}return e};return __assign.apply(this,arguments)};var __classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,i){if(r==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof t==="function"?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return r==="m"?i:r==="a"?i.call(e):i?i.value:t.get(e)};var __classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,i,a){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!a)throw new TypeError("Private accessor was defined without a setter");if(typeof t==="function"?e!==t||!a:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?a.call(e,r):a?a.value=r:t.set(e,r),r};var __spreadArray=this&&this.__spreadArray||function(e,t,r){if(r||arguments.length===2)for(var i=0,a=t.length,n;i<a;i++){if(n||!(i in t)){if(!n)n=Array.prototype.slice.call(t,0,i);n[i]=t[i]}}return e.concat(n||Array.prototype.slice.call(t))};export{CONST,STATE,AREA,elemId,theme,getClassFn,isUndef,omit,debounce,throttle};var CONST="constant";var STATE=Object.freeze({DEFAULT:"default",ACTIVE:"active",INACTIVE:"inactive"});var AREA=Object.freeze({WHOLE:"whole",MIDDLE:"middle",MAIN:"main",TOP:"top",LEFT:"left",RIGHT:"right",BOTTOM:"bottom",LABEL:"label",AUX:"aux",EXTRA:"extra"});var RandomId=function(){function e(){t.add(this);a.set(this,new Set)}e.prototype.get=function(e){if(!e)return;if(__classPrivateFieldGet(this,a,"f").size>1e4)__classPrivateFieldGet(this,a,"f").clear();return __classPrivateFieldGet(this,t,"m",l).call(this)};var t,r,i,a,n,s,l;r=e,a=new WeakMap,t=new WeakSet,n=function e(){return __classPrivateFieldGet(r,r,"f",i)[Math.trunc(Math.random()*__classPrivateFieldGet(r,r,"f",i).length)]},s=function e(){var r=this;return String.fromCharCode.apply(String,__spreadArray(__spreadArray([],Array(4).fill(null).map((function(){return __classPrivateFieldGet(r,t,"m",n).call(r)})),false),[58],false))},l=function e(){var r=__classPrivateFieldGet(this,t,"m",s).call(this);while(__classPrivateFieldGet(this,a,"f").has(r)){r=__classPrivateFieldGet(this,t,"m",s).call(this)}__classPrivateFieldGet(this,a,"f").add(r);return r};i={value:__spreadArray(__spreadArray([],Array.from(Array(25).keys(),(function(e){return e+65})),true),Array.from(Array(25).keys(),(function(e){return e+97})),true)};return e}();var ThemeSwitcher=function(){function e(){t.add(this);n.set(this,{});s.set(this,void 0);__classPrivateFieldSet(this,s,__classPrivateFieldGet(r,r,"m",d).call(r),"f")}Object.defineProperty(e.prototype,"current",{get:function(){return __classPrivateFieldGet(this,s,"f")},enumerable:false,configurable:true});e.prototype.setPreset=function(e){__classPrivateFieldSet(this,n,__classPrivateFieldGet(r,r,"m",u).call(r,e),"f");__classPrivateFieldGet(this,t,"m",o).call(this);return this};e.prototype.toLight=function(){this.switch(__classPrivateFieldGet(r,r,"f",a))};e.prototype.toDark=function(){this.switch(__classPrivateFieldGet(r,r,"f",i))};e.prototype.switch=function(e){if(!__classPrivateFieldGet(this,t,"m",c).call(this,e))return;__classPrivateFieldSet(this,s,e,"f");__classPrivateFieldGet(this,t,"m",l).call(this)};var t,r,i,a,n,s,l,c,o,u,f,d;r=e,n=new WeakMap,s=new WeakMap,t=new WeakSet,l=function e(){if(typeof window==="undefined")return;var t=window.document.body.style;Object.entries(__classPrivateFieldGet(this,n,"f")[__classPrivateFieldGet(this,s,"f")]).forEach((function(e){var r=e[0],i=e[1];return t.setProperty(r,i)}))},c=function e(t){return Object.keys(__classPrivateFieldGet(this,n,"f")).includes(t)},o=function e(){if(typeof window==="undefined")return;var t=Object.keys(__classPrivateFieldGet(this,n,"f")).filter((function(e){return e===__classPrivateFieldGet(r,r,"f",a)||e===__classPrivateFieldGet(r,r,"f",i)}));window.document.documentElement.style.colorScheme=t.join(" ")},u=function e(t){return Object.fromEntries(Object.entries(t).map((function(e){var t=e[0],i=e[1];return[t,__classPrivateFieldGet(r,r,"m",f).call(r,i)]})))},f=function e(t){return Object.fromEntries(Object.entries(t).map((function(e){var t=e[0],r=e[1];return["--".concat(t.replaceAll("_","-")),r]})))},d=function e(){if(typeof window==="undefined")return __classPrivateFieldGet(r,r,"f",a);return window.matchMedia("(prefers-color-scheme: light)").matches?__classPrivateFieldGet(r,r,"f",a):__classPrivateFieldGet(r,r,"f",i)};i={value:"dark"};a={value:"light"};return e}();var elemId=new RandomId;var theme=new ThemeSwitcher;function getClassFn(e,t,r){var i=getRule(e,t,r);if(typeof i==="string"){return function(e,t){return cssClass(i,e,t)}}else{return function(e,t){return ruleClass(i,e,t)}}}function getRule(e,t,r){if(typeof r==="string")return r.trim()?r:e;var i=mergeRule(t,r);return Object.keys(i).length<=0?e:i}function cssClass(e,t,r){return"".concat(e," ").concat(t).concat(r===STATE.DEFAULT?"":" ".concat(r))}function ruleClass(e,t,r){var i,a,n,s,l,c;var o=(a=(i=e[t])===null||i===void 0?void 0:i.constant)!==null&&a!==void 0?a:"";var u=(c=(s=(n=e[t])===null||n===void 0?void 0:n[r])!==null&&s!==void 0?s:(l=e[t])===null||l===void 0?void 0:l.default)!==null&&c!==void 0?c:"";return o===""&&u===""?undefined:"".concat(o).concat(o&&u?" ":"").concat(u)}function mergeRule(e,t){var r=Object.keys(e);if(r.length<=0)return t;var i=Object.keys(t);if(i.length<=0)return e;var a={};new Set(__spreadArray(__spreadArray([],r,true),i,true)).forEach((function(r){var i,n;a[r]=__assign(__assign({},(i=e[r])!==null&&i!==void 0?i:{}),(n=t[r])!==null&&n!==void 0?n:{})}));return a}function isUndef(e){return e===void 0}function omit(e){var t=[];for(var r=1;r<arguments.length;r++){t[r-1]=arguments[r]}if(Object.isFrozen(e)||Object.isSealed(e))return e;t.forEach((function(t){return delete e[t]}));return e}function debounce(e,t){var r;return function(){var i=[];for(var a=0;a<arguments.length;a++){i[a]=arguments[a]}if(r)clearTimeout(r);r=setTimeout((function(){t.call.apply(t,__spreadArray([null],i,false))}),e)}}function throttle(e,t){var r;var i=0;var a=function(){return Date.now()-i};var n=function(e){t.call.apply(t,__spreadArray([null],e,false));i=Date.now()};return function(){var t=[];for(var s=0;s<arguments.length;s++){t[s]=arguments[s]}if(!i)return n(t);clearTimeout(r);r=setTimeout((function(){if(a()>=e)n(t)}),e-a())}}
|
package/_svseeds/core.ts
CHANGED
|
@@ -93,7 +93,7 @@ class ThemeSwitcher {
|
|
|
93
93
|
this.#apply();
|
|
94
94
|
}
|
|
95
95
|
#apply() {
|
|
96
|
-
if (
|
|
96
|
+
if (typeof window === "undefined") return;
|
|
97
97
|
const style = window.document.body.style;
|
|
98
98
|
Object.entries(this.#styles[this.#current])
|
|
99
99
|
.forEach(([name, value]) => style.setProperty(name, value));
|
|
@@ -102,7 +102,7 @@ class ThemeSwitcher {
|
|
|
102
102
|
return Object.keys(this.#styles).includes(theme);
|
|
103
103
|
}
|
|
104
104
|
#setColorScheme() {
|
|
105
|
-
if (
|
|
105
|
+
if (typeof window === "undefined") return;
|
|
106
106
|
const themes = Object.keys(this.#styles).filter((x) => x === ThemeSwitcher.#LIGHT || x === ThemeSwitcher.#DARK);
|
|
107
107
|
window.document.documentElement.style.colorScheme = themes.join(" ");
|
|
108
108
|
}
|
|
@@ -120,7 +120,8 @@ class ThemeSwitcher {
|
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
122
|
static #setInitialTheme(): string {
|
|
123
|
-
|
|
123
|
+
if (typeof window === "undefined") return ThemeSwitcher.#LIGHT;
|
|
124
|
+
return window.matchMedia("(prefers-color-scheme: light)").matches ? ThemeSwitcher.#LIGHT : ThemeSwitcher.#DARK;
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
const elemId = new RandomId();
|
package/package.json
CHANGED