@nano_kit/react 1.0.0-alpha.3

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 ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 - present, TrigenSoftware
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 ADDED
@@ -0,0 +1,63 @@
1
+ # @nano_kit/react
2
+
3
+ [![ESM-only package][package]][package-url]
4
+ [![NPM version][npm]][npm-url]
5
+ [![Dependencies status][deps]][deps-url]
6
+ [![Install size][size]][size-url]
7
+ [![Build status][build]][build-url]
8
+ [![Coverage status][coverage]][coverage-url]
9
+
10
+ [package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
11
+ [package-url]: https://nodejs.org/api/esm.html
12
+
13
+ [npm]: https://img.shields.io/npm/v/%40nano_kit%2Freact.svg
14
+ [npm-url]: https://npmjs.com/package/@nano_kit/react
15
+
16
+ [deps]: https://img.shields.io/librariesio/release/npm/%40nano_kit%2Freact
17
+ [deps-url]: https://libraries.io/npm/%40nano_kit%2Freact/tree
18
+
19
+ [size]: https://deno.bundlejs.com/badge?q=%40nano_kit%2Freact
20
+ [size-url]: https://bundlejs.com/?q=%40nano_kit%2Freact
21
+
22
+ [build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/nano_kit/tests.yml?branch=main
23
+ [build-url]: https://github.com/TrigenSoftware/nano_kit/actions
24
+
25
+ [coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/nano_kit.svg
26
+ [coverage-url]: https://app.codecov.io/gh/TrigenSoftware/nano_kit
27
+
28
+ The `@nano_kit/react` package provides seamless integration between [@nano_kit/store](../store) signals, dependency injection and React components.
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ pnpm add @nano_kit/store @nano_kit/react
34
+ # or
35
+ npm install @nano_kit/store @nano_kit/react
36
+ # or
37
+ yarn add @nano_kit/store @nano_kit/react
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ Here is a minimal example demonstrating reactive state in React:
43
+
44
+ ```tsx
45
+ import { signal } from '@nano_kit/store'
46
+ import { useSignal } from '@nano_kit/react'
47
+
48
+ const $count = signal(0)
49
+
50
+ export function Counter() {
51
+ const count = useSignal($count)
52
+
53
+ return (
54
+ <button onClick={() => $count(count + 1)}>
55
+ Count: {count}
56
+ </button>
57
+ )
58
+ }
59
+ ```
60
+
61
+ ## Documentation
62
+
63
+ For comprehensive guides, API reference, and integration patterns, visit the [documentation website](https://nano_kit.js.org/integrations/react).
@@ -0,0 +1,44 @@
1
+ import { type ReadableSignal, type InjectionProvider, type InjectionFactory, InjectionContext } from '@nano_kit/store';
2
+ import { type ReactNode } from 'react';
3
+ /**
4
+ * Get signal store value and subscribe to changes.
5
+ * @param $signal - The signal store.
6
+ * @returns The signal store value.
7
+ */
8
+ export declare function useSignal<T>($signal: ReadableSignal<T>): T;
9
+ export declare const ReactInjectionContext: import("react").Context<InjectionContext | undefined>;
10
+ /**
11
+ * Inject a dependency.
12
+ * @param factory - The factory function to create or get the dependency.
13
+ * @returns The dependency.
14
+ */
15
+ export declare function useInject<T>(factory: InjectionFactory<T>): T;
16
+ /**
17
+ * Create a hook to inject a dependency.
18
+ * @param factory - The factory function to create or get the dependency.
19
+ * @returns A hook function to get the dependency.
20
+ */
21
+ export declare function hook<T>(factory: InjectionFactory<T>): () => T;
22
+ export interface InjectionContextProps {
23
+ context?: InjectionContext | InjectionProvider[];
24
+ children: ReactNode;
25
+ }
26
+ /**
27
+ * Provide dependencies to children.
28
+ * @param props
29
+ * @param props.context - The dependencies to provide or InjectionContext instance.
30
+ * @param props.children - The children to provide the dependencies to.
31
+ * @returns The component.
32
+ */
33
+ export declare function InjectionContextProvider({ context, children }: InjectionContextProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react").JSX.Element | null | undefined;
34
+ export interface IsolateProps {
35
+ children: ReactNode;
36
+ }
37
+ /**
38
+ * Isolate children to new isolated injection context.
39
+ * @param props
40
+ * @param props.children - The children to isolate.
41
+ * @returns The component.
42
+ */
43
+ export declare function Isolate({ children }: IsolateProps): import("react").JSX.Element;
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,gBAAgB,EAGjB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACL,KAAK,SAAS,EAOf,MAAM,OAAO,CAAA;AAEd;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,KAkBtD;AAED,eAAO,MAAM,qBAAqB,uDAAyE,CAAA;AAE3G;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,CAAC,CAQ5D;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAE7D;AAED,MAAM,WAAW,qBAAqB;IACpC,OAAO,CAAC,EAAE,gBAAgB,GAAG,iBAAiB,EAAE,CAAA;IAChD,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,OAAO,EACP,QAAQ,EACT,EAAE,qBAAqB,+TAiBvB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,QAAQ,EAAE,EAAE,YAAY,+BAMjD"}
package/dist/index.js ADDED
@@ -0,0 +1,50 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { effect, inject, InjectionContext } from '@nano_kit/store';
3
+ import { createContext, useRef, useCallback, useSyncExternalStore, useContext, useMemo } from 'react';
4
+
5
+ function useSignal($signal) {
6
+ const snapshotRef = useRef(void 0);
7
+ const sub = useCallback(
8
+ (emit) => effect(() => {
9
+ const value = $signal();
10
+ if (snapshotRef.current !== value) {
11
+ snapshotRef.current = value;
12
+ emit();
13
+ }
14
+ }),
15
+ [$signal]
16
+ );
17
+ const get = () => snapshotRef.current;
18
+ snapshotRef.current = $signal();
19
+ return useSyncExternalStore(sub, get, get);
20
+ }
21
+ const ReactInjectionContext = /* @__PURE__ */ createContext(void 0);
22
+ function useInject(factory) {
23
+ const currentContext = useContext(ReactInjectionContext);
24
+ const dependency = useMemo(
25
+ () => inject(factory, currentContext),
26
+ [currentContext, factory]
27
+ );
28
+ return dependency;
29
+ }
30
+ function hook(factory) {
31
+ return () => useInject(factory);
32
+ }
33
+ function InjectionContextProvider({
34
+ context,
35
+ children
36
+ }) {
37
+ const currentContext = useContext(ReactInjectionContext);
38
+ const contextRef = useRef(null);
39
+ if (!context && currentContext) {
40
+ return children;
41
+ }
42
+ const value = contextRef.current ??= context instanceof InjectionContext ? context : new InjectionContext(context, currentContext);
43
+ return /* @__PURE__ */ jsx(ReactInjectionContext.Provider, { value, children });
44
+ }
45
+ function Isolate({ children }) {
46
+ return /* @__PURE__ */ jsx(ReactInjectionContext.Provider, { value: void 0, children });
47
+ }
48
+
49
+ export { InjectionContextProvider, Isolate, ReactInjectionContext, hook, useInject, useSignal };
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.tsx"],"sourcesContent":["import {\n type ReadableSignal,\n type InjectionProvider,\n type InjectionFactory,\n InjectionContext,\n inject,\n effect\n} from '@nano_kit/store'\nimport {\n type ReactNode,\n useCallback,\n useMemo,\n useRef,\n useSyncExternalStore,\n createContext,\n useContext\n} from 'react'\n\n/**\n * Get signal store value and subscribe to changes.\n * @param $signal - The signal store.\n * @returns The signal store value.\n */\nexport function useSignal<T>($signal: ReadableSignal<T>) {\n const snapshotRef = useRef(undefined as T)\n const sub = useCallback(\n (emit: () => void) => effect(() => {\n const value = $signal()\n\n if (snapshotRef.current !== value) {\n snapshotRef.current = value\n emit()\n }\n }),\n [$signal]\n )\n const get = () => snapshotRef.current\n\n snapshotRef.current = $signal()\n\n return useSyncExternalStore(sub, get, get)\n}\n\nexport const ReactInjectionContext = /* @__PURE__ */ createContext<InjectionContext | undefined>(undefined)\n\n/**\n * Inject a dependency.\n * @param factory - The factory function to create or get the dependency.\n * @returns The dependency.\n */\nexport function useInject<T>(factory: InjectionFactory<T>): T {\n const currentContext = useContext(ReactInjectionContext)\n const dependency = useMemo(\n () => inject(factory, currentContext),\n [currentContext, factory]\n )\n\n return dependency\n}\n\n/**\n * Create a hook to inject a dependency.\n * @param factory - The factory function to create or get the dependency.\n * @returns A hook function to get the dependency.\n */\nexport function hook<T>(factory: InjectionFactory<T>): () => T {\n return () => useInject(factory)\n}\n\nexport interface InjectionContextProps {\n context?: InjectionContext | InjectionProvider[]\n children: ReactNode\n}\n\n/**\n * Provide dependencies to children.\n * @param props\n * @param props.context - The dependencies to provide or InjectionContext instance.\n * @param props.children - The children to provide the dependencies to.\n * @returns The component.\n */\nexport function InjectionContextProvider({\n context,\n children\n}: InjectionContextProps) {\n const currentContext = useContext(ReactInjectionContext)\n const contextRef = useRef<InjectionContext>(null)\n\n if (!context && currentContext) {\n return children\n }\n\n const value = contextRef.current ??= context instanceof InjectionContext\n ? context\n : new InjectionContext(context, currentContext)\n\n return (\n <ReactInjectionContext.Provider value={value}>\n {children}\n </ReactInjectionContext.Provider>\n )\n}\n\nexport interface IsolateProps {\n children: ReactNode\n}\n\n/**\n * Isolate children to new isolated injection context.\n * @param props\n * @param props.children - The children to isolate.\n * @returns The component.\n */\nexport function Isolate({ children }: IsolateProps) {\n return (\n <ReactInjectionContext.Provider value={undefined}>\n {children}\n </ReactInjectionContext.Provider>\n )\n}\n"],"names":[],"mappings":";;;;AAuBO,SAAS,UAAa,OAAA,EAA4B;AACvD,EAAA,MAAM,WAAA,GAAc,OAAO,MAAc,CAAA;AACzC,EAAA,MAAM,GAAA,GAAM,WAAA;AAAA,IACV,CAAC,IAAA,KAAqB,MAAA,CAAO,MAAM;AACjC,MAAA,MAAM,QAAQ,OAAA,EAAQ;AAEtB,MAAA,IAAI,WAAA,CAAY,YAAY,KAAA,EAAO;AACjC,QAAA,WAAA,CAAY,OAAA,GAAU,KAAA;AACtB,QAAA,IAAA,EAAK;AAAA,MACP;AAAA,IACF,CAAC,CAAA;AAAA,IACD,CAAC,OAAO;AAAA,GACV;AACA,EAAA,MAAM,GAAA,GAAM,MAAM,WAAA,CAAY,OAAA;AAE9B,EAAA,WAAA,CAAY,UAAU,OAAA,EAAQ;AAE9B,EAAA,OAAO,oBAAA,CAAqB,GAAA,EAAK,GAAA,EAAK,GAAG,CAAA;AAC3C;AAEO,MAAM,qBAAA,iCAAoF,MAAS;AAOnG,SAAS,UAAa,OAAA,EAAiC;AAC5D,EAAA,MAAM,cAAA,GAAiB,WAAW,qBAAqB,CAAA;AACvD,EAAA,MAAM,UAAA,GAAa,OAAA;AAAA,IACjB,MAAM,MAAA,CAAO,OAAA,EAAS,cAAc,CAAA;AAAA,IACpC,CAAC,gBAAgB,OAAO;AAAA,GAC1B;AAEA,EAAA,OAAO,UAAA;AACT;AAOO,SAAS,KAAQ,OAAA,EAAuC;AAC7D,EAAA,OAAO,MAAM,UAAU,OAAO,CAAA;AAChC;AAcO,SAAS,wBAAA,CAAyB;AAAA,EACvC,OAAA;AAAA,EACA;AACF,CAAA,EAA0B;AACxB,EAAA,MAAM,cAAA,GAAiB,WAAW,qBAAqB,CAAA;AACvD,EAAA,MAAM,UAAA,GAAa,OAAyB,IAAI,CAAA;AAEhD,EAAA,IAAI,CAAC,WAAW,cAAA,EAAgB;AAC9B,IAAA,OAAO,QAAA;AAAA,EACT;AAEA,EAAA,MAAM,KAAA,GAAQ,WAAW,OAAA,KAAY,OAAA,YAAmB,mBACpD,OAAA,GACA,IAAI,gBAAA,CAAiB,OAAA,EAAS,cAAc,CAAA;AAEhD,EAAA,uBACE,GAAA,CAAC,qBAAA,CAAsB,QAAA,EAAtB,EAA+B,OAC7B,QAAA,EACH,CAAA;AAEJ;AAYO,SAAS,OAAA,CAAQ,EAAE,QAAA,EAAS,EAAiB;AAClD,EAAA,2BACG,qBAAA,CAAsB,QAAA,EAAtB,EAA+B,KAAA,EAAO,QACpC,QAAA,EACH,CAAA;AAEJ;;;;"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@nano_kit/react",
3
+ "type": "module",
4
+ "version": "1.0.0-alpha.3",
5
+ "description": "Seamless integration between @nano_kit/store signals, dependency injection and React components.",
6
+ "author": "dangreen",
7
+ "license": "MIT",
8
+ "homepage": "https://nano_kit.js.org/integrations/react",
9
+ "funding": "https://ko-fi.com/dangreen",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/TrigenSoftware/nano_kit.git",
13
+ "directory": "packages/react"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/TrigenSoftware/nano_kit/issues"
17
+ },
18
+ "keywords": [
19
+ "react",
20
+ "store",
21
+ "state",
22
+ "state-management",
23
+ "signal",
24
+ "reactive",
25
+ "hooks",
26
+ "nano_kit"
27
+ ],
28
+ "engines": {
29
+ "node": ">=16"
30
+ },
31
+ "sideEffects": false,
32
+ "exports": {
33
+ "./package.json": "./package.json",
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "default": "./dist/index.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "peerDependencies": {
43
+ "react": "^17.0.0 || ^18.0.0 || ^19.0.0",
44
+ "@nano_kit/store": "^1.0.0-alpha.0"
45
+ }
46
+ }