@marianmeres/stuic 2.0.3 → 2.0.5

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.
@@ -3,4 +3,5 @@ export declare function autogrow(el: HTMLTextAreaElement, fn?: () => {
3
3
  min?: number;
4
4
  max?: number;
5
5
  immediate?: boolean;
6
+ value?: string;
6
7
  }): void;
@@ -1,16 +1,30 @@
1
1
  export function autogrow(el, fn) {
2
+ let lastValue = el.value;
2
3
  $effect(() => {
3
- let { enabled = true, max = 250, immediate = true } = fn?.() || {};
4
- function set_height(e) {
4
+ let { enabled = true, max = 250, immediate = true, value } = fn?.() || {};
5
+ if (!enabled)
6
+ return;
7
+ function set_height(_e) {
8
+ // console.log(123, el.value);
5
9
  if (enabled) {
6
10
  el.style.height = "auto"; // Reset height to auto to correctly calculate scrollHeight
7
11
  el.style.height = Math.min(el.scrollHeight, max) + "px";
8
12
  }
9
13
  }
10
- if (immediate)
11
- set_height(null);
12
- el.addEventListener("input", set_height);
13
- el.addEventListener("blur", set_height);
14
+ // eventlistener strategy (we're not passing value)
15
+ if (value === undefined) {
16
+ if (immediate)
17
+ set_height(null);
18
+ el.addEventListener("input", set_height);
19
+ el.addEventListener("blur", set_height);
20
+ }
21
+ // strategy with provided value
22
+ else {
23
+ if (value !== lastValue) {
24
+ set_height(null);
25
+ lastValue = value;
26
+ }
27
+ }
14
28
  return () => {
15
29
  el.removeEventListener("input", set_height);
16
30
  el.removeEventListener("blur", set_height);
@@ -137,6 +137,7 @@
137
137
  use:autogrow={() => ({
138
138
  enabled: !!useAutogrow,
139
139
  ...(typeof useAutogrow === "boolean" ? {} : useAutogrow),
140
+ value,
140
141
  })}
141
142
  {tabindex}
142
143
  {required}
@@ -7,6 +7,7 @@ export * from "./event-modifiers.js";
7
7
  export * from "./get-id.js";
8
8
  export * from "./is-browser.js";
9
9
  export * from "./is-mac.js";
10
+ export * from "./is-nullish.js";
10
11
  export * from "./maybe-json-parse.js";
11
12
  export * from "./maybe-json-stringify.js";
12
13
  export * from "./omit-pick.js";
@@ -19,6 +20,7 @@ export * from "./seconds.js";
19
20
  export * from "./sleep.js";
20
21
  export * from "./storage-abstraction.js";
21
22
  export * from "./str-hash.js";
23
+ export * from "./switch.svelte.js";
22
24
  export * from "./throttle.js";
23
25
  export * from "./tr.js";
24
26
  export * from "./tw-merge.js";
@@ -7,6 +7,7 @@ export * from "./event-modifiers.js";
7
7
  export * from "./get-id.js";
8
8
  export * from "./is-browser.js";
9
9
  export * from "./is-mac.js";
10
+ export * from "./is-nullish.js";
10
11
  export * from "./maybe-json-parse.js";
11
12
  export * from "./maybe-json-stringify.js";
12
13
  export * from "./omit-pick.js";
@@ -19,6 +20,7 @@ export * from "./seconds.js";
19
20
  export * from "./sleep.js";
20
21
  export * from "./storage-abstraction.js";
21
22
  export * from "./str-hash.js";
23
+ export * from "./switch.svelte.js";
22
24
  export * from "./throttle.js";
23
25
  export * from "./tr.js";
24
26
  export * from "./tw-merge.js";
@@ -0,0 +1 @@
1
+ export declare function isNullish(v: any): boolean;
@@ -0,0 +1,3 @@
1
+ export function isNullish(v) {
2
+ return v === null || v === undefined;
3
+ }
@@ -0,0 +1,20 @@
1
+ /** 3 state (true, false, undefined) DRY helper */
2
+ export declare class SwitchState<T> {
3
+ #private;
4
+ readonly key: string;
5
+ readonly storageType: "memory" | "local" | "session";
6
+ onOff: ((data: T, self: SwitchState<T>) => void) | undefined | null;
7
+ constructor(key: string, initial?: boolean | null, storageType?: "memory" | "local" | "session", initialData?: any);
8
+ __set(value: boolean | null, data?: T | null | undefined): void;
9
+ on(data?: T | null | undefined): void;
10
+ off(data?: T | null | undefined): void;
11
+ toggle(data?: T | null | undefined): void;
12
+ reset(data?: T | null | undefined): void;
13
+ get isOn(): boolean;
14
+ get isOff(): boolean;
15
+ get isUndefined(): boolean;
16
+ get current(): boolean | null;
17
+ get data(): T;
18
+ set data(data: T);
19
+ addData(data: T): void;
20
+ }
@@ -0,0 +1,84 @@
1
+ import { localStorageValue, memoryStorageValue, sessionStorageValue, } from "./storage-abstraction.js";
2
+ const storageTypeFactory = {
3
+ local: localStorageValue,
4
+ session: sessionStorageValue,
5
+ memory: memoryStorageValue,
6
+ };
7
+ /** 3 state (true, false, undefined) DRY helper */
8
+ export class SwitchState {
9
+ key;
10
+ storageType;
11
+ // actual switch value (1 of 3 possible). Note that outer `undefined` is always inner `null`.
12
+ #current = $state(null);
13
+ // arbitrary data associated with the switch
14
+ #data = $state(null);
15
+ #storage;
16
+ onOff = null;
17
+ constructor(key, initial = null, storageType = "memory", initialData = null) {
18
+ this.key = key;
19
+ this.storageType = storageType;
20
+ this.#current = initial;
21
+ this.#data = initialData;
22
+ if (typeof storageTypeFactory[this.storageType] === "function") {
23
+ this.#storage = storageTypeFactory[this.storageType](this.key, {
24
+ value: initial,
25
+ data: null,
26
+ });
27
+ }
28
+ else {
29
+ console.warn(`Unknown storageType "${this.storageType}"`);
30
+ }
31
+ }
32
+ // still public, but should not be used directly unless necessary for some reason
33
+ __set(value, data) {
34
+ if (value !== null && typeof value !== "boolean")
35
+ value = Boolean(value);
36
+ this.#current = value;
37
+ // only defined data will be set
38
+ if (data !== undefined)
39
+ this.#data = data;
40
+ // mirror to storage
41
+ this.#storage?.set({ value, data });
42
+ // if we're closing fire (once) onOff if exists
43
+ if (!value) {
44
+ this.onOff?.(this.data, this);
45
+ this.onOff = null;
46
+ }
47
+ }
48
+ on(data) {
49
+ this.__set(true, data);
50
+ }
51
+ off(data) {
52
+ this.__set(false, data);
53
+ }
54
+ toggle(data) {
55
+ this.__set(!this.#current, data);
56
+ }
57
+ reset(data) {
58
+ this.__set(null, data);
59
+ }
60
+ get isOn() {
61
+ return this.#current === true;
62
+ }
63
+ get isOff() {
64
+ // both `false` or `null`
65
+ return this.#current !== true;
66
+ }
67
+ // the outer "undefined" value is the 3rd state of the switch (internally the raw
68
+ // value is always `null`)
69
+ get isUndefined() {
70
+ return this.#current === null;
71
+ }
72
+ get current() {
73
+ return this.#current;
74
+ }
75
+ get data() {
76
+ return this.#data;
77
+ }
78
+ set data(data) {
79
+ this.__set(this.#current, data);
80
+ }
81
+ addData(data) {
82
+ this.data = { ...(this.data || {}), ...data };
83
+ }
84
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/stuic",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run prepack",