simplestyle-js 5.4.4 → 5.4.5-beta.1

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.
@@ -9,12 +9,26 @@ Object.defineProperty(exports, "SimpleStyleProvider", {
9
9
  }
10
10
  });
11
11
  const _jsxruntime = require("react/jsx-runtime");
12
+ const _react = require("react");
12
13
  const _ClientBoundary = require("./ClientBoundary.cjs");
13
14
  function SimpleStyleProvider({ children, registry }) {
15
+ /** state */ const [rules, setRules] = (0, _react.useState)(null);
16
+ /** effects */ (0, _react.useEffect)(()=>{
17
+ const handleSettled = ()=>{
18
+ setRules(registry.getRulesById());
19
+ };
20
+ registry.on('settled', handleSettled);
21
+ return ()=>{
22
+ registry.off('settled', handleSettled);
23
+ };
24
+ }, [
25
+ registry
26
+ ]);
27
+ if (!rules) return null;
14
28
  return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
15
29
  children: [
16
30
  /*#__PURE__*/ (0, _jsxruntime.jsx)(_ClientBoundary.ClientBoundary, {
17
- rules: registry.getRulesById()
31
+ rules: rules
18
32
  }),
19
33
  children
20
34
  ]
@@ -1,4 +1,4 @@
1
- import type { PropsWithChildren } from 'react';
1
+ import { type PropsWithChildren } from 'react';
2
2
  import type { SimpleStyleRegistry } from '../simpleStyleRegistry.js';
3
3
  /**
4
4
  * Accumulates all CSS rules and writes
@@ -8,4 +8,4 @@ import type { SimpleStyleRegistry } from '../simpleStyleRegistry.js';
8
8
  */
9
9
  export declare function SimpleStyleProvider({ children, registry, }: PropsWithChildren & {
10
10
  registry: SimpleStyleRegistry;
11
- }): import("react/jsx-runtime").JSX.Element;
11
+ }): import("react/jsx-runtime").JSX.Element | null;
@@ -11,6 +11,8 @@ Object.defineProperty(exports, "SimpleStyleRegistry", {
11
11
  /* eslint-disable unicorn/prefer-query-selector */ const doc = globalThis.document;
12
12
  class SimpleStyleRegistry {
13
13
  sheets = new Map();
14
+ callbacks = [];
15
+ addTimeout = null;
14
16
  add(ruleId, contents) {
15
17
  if (this.sheets.has(ruleId) && doc) {
16
18
  const tag = doc.getElementById(ruleId);
@@ -19,6 +21,21 @@ class SimpleStyleRegistry {
19
21
  }
20
22
  }
21
23
  this.sheets.set(ruleId, contents);
24
+ this.callbacks.forEach((entry)=>{
25
+ if (entry.eventName === 'add') {
26
+ entry.callback.call(this, ruleId, contents);
27
+ }
28
+ });
29
+ if (this.addTimeout) {
30
+ clearTimeout(this.addTimeout);
31
+ }
32
+ this.addTimeout = setTimeout(()=>{
33
+ this.callbacks.forEach((entry)=>{
34
+ if (entry.eventName === 'settled') {
35
+ entry.callback.call(this);
36
+ }
37
+ });
38
+ }, 50);
22
39
  }
23
40
  /**
24
41
  * returns the bare CSS to be injected into a <style /> tag
@@ -45,4 +62,22 @@ ${contents}`, '');
45
62
  ...this.sheets.entries()
46
63
  ];
47
64
  }
65
+ /**
66
+ * bind an event handler to one of the various events
67
+ * that occur on the registry
68
+ */ on(eventName, callback) {
69
+ // @ts-expect-error - gross typings
70
+ this.callbacks.push({
71
+ eventName,
72
+ callback
73
+ });
74
+ }
75
+ /**
76
+ * disconnects an event handler from the registry.
77
+ * if no callback is specified when calling off,
78
+ * all event handlers are unbound
79
+ */ off(eventName, callback) {
80
+ const cbIsFunc = typeof callback === 'function';
81
+ this.callbacks = this.callbacks.filter((entry)=>entry.eventName !== eventName && (cbIsFunc && entry.callback !== callback || !cbIsFunc));
82
+ }
48
83
  }
@@ -1,3 +1,6 @@
1
+ type AnyEventName = 'add' | 'settled';
2
+ export type SimpleStyleRegistryAddEventCallback = (this: SimpleStyleRegistry, ruleId: string, contents: string) => void;
3
+ export type SimpleStyleRegistrySettledEventCallback = (this: SimpleStyleRegistry) => void;
1
4
  /**
2
5
  * Acts as an accumulator for all
3
6
  * generated css that occurs via createStyles().
@@ -6,6 +9,8 @@
6
9
  */
7
10
  export declare class SimpleStyleRegistry {
8
11
  private sheets;
12
+ private callbacks;
13
+ private addTimeout;
9
14
  add(ruleId: string, contents: string): void;
10
15
  /**
11
16
  * returns the bare CSS to be injected into a <style /> tag
@@ -25,4 +30,16 @@ export declare class SimpleStyleRegistry {
25
30
  * won't work during local development
26
31
  */
27
32
  getRulesById(): [string, string][];
33
+ /**
34
+ * bind an event handler to one of the various events
35
+ * that occur on the registry
36
+ */
37
+ on<E extends AnyEventName, Cb = E extends 'add' ? SimpleStyleRegistryAddEventCallback : E extends 'settled' ? SimpleStyleRegistrySettledEventCallback : never>(eventName: E, callback: Cb): void;
38
+ /**
39
+ * disconnects an event handler from the registry.
40
+ * if no callback is specified when calling off,
41
+ * all event handlers are unbound
42
+ */
43
+ off<E extends AnyEventName, Cb = E extends 'add' ? SimpleStyleRegistryAddEventCallback : E extends 'settled' ? SimpleStyleRegistrySettledEventCallback : never>(eventName: E, callback?: Cb): void;
28
44
  }
45
+ export {};
@@ -1,4 +1,4 @@
1
- import type { PropsWithChildren } from 'react';
1
+ import { type PropsWithChildren } from 'react';
2
2
  import type { SimpleStyleRegistry } from '../simpleStyleRegistry.js';
3
3
  /**
4
4
  * Accumulates all CSS rules and writes
@@ -8,4 +8,4 @@ import type { SimpleStyleRegistry } from '../simpleStyleRegistry.js';
8
8
  */
9
9
  export declare function SimpleStyleProvider({ children, registry, }: PropsWithChildren & {
10
10
  registry: SimpleStyleRegistry;
11
- }): import("react/jsx-runtime").JSX.Element;
11
+ }): import("react/jsx-runtime").JSX.Element | null;
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useEffect, useState } from 'react';
2
3
  import { ClientBoundary } from './ClientBoundary.mjs';
3
4
  /**
4
5
  * Accumulates all CSS rules and writes
@@ -6,10 +7,23 @@ import { ClientBoundary } from './ClientBoundary.mjs';
6
7
  * Use this for Next.js or other Next.js-like frameworks
7
8
  * that leverage React server components
8
9
  */ export function SimpleStyleProvider({ children, registry }) {
10
+ /** state */ const [rules, setRules] = useState(null);
11
+ /** effects */ useEffect(()=>{
12
+ const handleSettled = ()=>{
13
+ setRules(registry.getRulesById());
14
+ };
15
+ registry.on('settled', handleSettled);
16
+ return ()=>{
17
+ registry.off('settled', handleSettled);
18
+ };
19
+ }, [
20
+ registry
21
+ ]);
22
+ if (!rules) return null;
9
23
  return /*#__PURE__*/ _jsxs(_Fragment, {
10
24
  children: [
11
25
  /*#__PURE__*/ _jsx(ClientBoundary, {
12
- rules: registry.getRulesById()
26
+ rules: rules
13
27
  }),
14
28
  children
15
29
  ]
@@ -1,3 +1,6 @@
1
+ type AnyEventName = 'add' | 'settled';
2
+ export type SimpleStyleRegistryAddEventCallback = (this: SimpleStyleRegistry, ruleId: string, contents: string) => void;
3
+ export type SimpleStyleRegistrySettledEventCallback = (this: SimpleStyleRegistry) => void;
1
4
  /**
2
5
  * Acts as an accumulator for all
3
6
  * generated css that occurs via createStyles().
@@ -6,6 +9,8 @@
6
9
  */
7
10
  export declare class SimpleStyleRegistry {
8
11
  private sheets;
12
+ private callbacks;
13
+ private addTimeout;
9
14
  add(ruleId: string, contents: string): void;
10
15
  /**
11
16
  * returns the bare CSS to be injected into a <style /> tag
@@ -25,4 +30,16 @@ export declare class SimpleStyleRegistry {
25
30
  * won't work during local development
26
31
  */
27
32
  getRulesById(): [string, string][];
33
+ /**
34
+ * bind an event handler to one of the various events
35
+ * that occur on the registry
36
+ */
37
+ on<E extends AnyEventName, Cb = E extends 'add' ? SimpleStyleRegistryAddEventCallback : E extends 'settled' ? SimpleStyleRegistrySettledEventCallback : never>(eventName: E, callback: Cb): void;
38
+ /**
39
+ * disconnects an event handler from the registry.
40
+ * if no callback is specified when calling off,
41
+ * all event handlers are unbound
42
+ */
43
+ off<E extends AnyEventName, Cb = E extends 'add' ? SimpleStyleRegistryAddEventCallback : E extends 'settled' ? SimpleStyleRegistrySettledEventCallback : never>(eventName: E, callback?: Cb): void;
28
44
  }
45
+ export {};
@@ -6,6 +6,8 @@
6
6
  * React variant of this will come after the baseline one is created
7
7
  */ export class SimpleStyleRegistry {
8
8
  sheets = new Map();
9
+ callbacks = [];
10
+ addTimeout = null;
9
11
  add(ruleId, contents) {
10
12
  if (this.sheets.has(ruleId) && doc) {
11
13
  const tag = doc.getElementById(ruleId);
@@ -14,6 +16,21 @@
14
16
  }
15
17
  }
16
18
  this.sheets.set(ruleId, contents);
19
+ this.callbacks.forEach((entry)=>{
20
+ if (entry.eventName === 'add') {
21
+ entry.callback.call(this, ruleId, contents);
22
+ }
23
+ });
24
+ if (this.addTimeout) {
25
+ clearTimeout(this.addTimeout);
26
+ }
27
+ this.addTimeout = setTimeout(()=>{
28
+ this.callbacks.forEach((entry)=>{
29
+ if (entry.eventName === 'settled') {
30
+ entry.callback.call(this);
31
+ }
32
+ });
33
+ }, 50);
17
34
  }
18
35
  /**
19
36
  * returns the bare CSS to be injected into a <style /> tag
@@ -40,4 +57,22 @@ ${contents}`, '');
40
57
  ...this.sheets.entries()
41
58
  ];
42
59
  }
60
+ /**
61
+ * bind an event handler to one of the various events
62
+ * that occur on the registry
63
+ */ on(eventName, callback) {
64
+ // @ts-expect-error - gross typings
65
+ this.callbacks.push({
66
+ eventName,
67
+ callback
68
+ });
69
+ }
70
+ /**
71
+ * disconnects an event handler from the registry.
72
+ * if no callback is specified when calling off,
73
+ * all event handlers are unbound
74
+ */ off(eventName, callback) {
75
+ const cbIsFunc = typeof callback === 'function';
76
+ this.callbacks = this.callbacks.filter((entry)=>entry.eventName !== eventName && (cbIsFunc && entry.callback !== callback || !cbIsFunc));
77
+ }
43
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplestyle-js",
3
- "version": "5.4.4",
3
+ "version": "5.4.5-beta.1",
4
4
  "description": "An incredibly straightforward and simple CSS-in-JS solution with zero runtime dependencies, and out-of-the-box TypeScript support",
5
5
  "type": "module",
6
6
  "repository": {