@inkweave/react 1.0.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.
@@ -0,0 +1,18 @@
1
+ import { FC } from 'react';
2
+ export interface ChoiceComponentProps {
3
+ val?: string;
4
+ onClick: () => void;
5
+ className?: string;
6
+ children?: React.ReactNode;
7
+ }
8
+ type ChoiceComponent = FC<ChoiceComponentProps>;
9
+ declare class ChoiceComponentsClass {
10
+ private _components;
11
+ get(type: string): ChoiceComponent | undefined;
12
+ register(type: string, component: ChoiceComponent): void;
13
+ unregister(type: string): void;
14
+ clear(): void;
15
+ has(type: string): boolean;
16
+ }
17
+ export declare const ChoiceComponents: ChoiceComponentsClass;
18
+ export {};
@@ -0,0 +1,6 @@
1
+ interface ChoicesProps {
2
+ onClick?: (index: number) => void;
3
+ className?: string;
4
+ }
5
+ declare const _default: import('react').NamedExoticComponent<ChoicesProps>;
6
+ export default _default;
@@ -0,0 +1,6 @@
1
+ interface ContentsProps {
2
+ lineDelay?: number;
3
+ className?: string;
4
+ }
5
+ declare const _default: import('react').NamedExoticComponent<ContentsProps>;
6
+ export default _default;
@@ -0,0 +1,15 @@
1
+ import { InkStory } from '@inkweave/core';
2
+ export declare const useStory: () => InkStory;
3
+ interface StoryProviderProps {
4
+ ink: InkStory;
5
+ children: React.ReactNode;
6
+ }
7
+ export declare const StoryProvider: React.FC<StoryProviderProps>;
8
+ interface StoryProps {
9
+ ink: InkStory;
10
+ children?: React.ReactNode;
11
+ className?: string;
12
+ onInit?: (ink: InkStory) => void;
13
+ }
14
+ declare const _default: import('react').NamedExoticComponent<StoryProps>;
15
+ export default _default;
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var S=Object.defineProperty;var j=(e,t,n)=>t in e?S(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var C=(e,t,n)=>j(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react/jsx-runtime"),c=require("react"),u=require("@inkweave/core"),p=c.createContext(null),h=()=>{const e=c.useContext(p);if(!e)throw new Error("useStory must be used within StoryProvider");return e},v=({ink:e,children:t})=>o.jsx(p.Provider,{value:e,children:t}),w=({ink:e,children:t,className:n="",onInit:l})=>{const s=c.useRef(l);return s.current=l,c.useEffect(()=>{var i;e.restart(),(i=s.current)==null||i.call(s,e)},[e]),o.jsx(v,{ink:e,children:o.jsx("div",{id:"inkweave-story",className:n,children:t})})},g=c.memo(w),f=({lineDelay:e=.05,className:t=""})=>{const n=h(),l=u.contentsStore(a=>a.contents),s=n,i=(s==null?void 0:s.visibleLines)!=null?s.visibleLines:l.length;return o.jsx("section",{id:"inkweave-contents",children:l.map((a,r)=>{const d={"--delay":`${(r>i?r-i:0)*e}s`},m=a===u.CHOICE_SEPARATOR,y=m?`divider_${r}`:`line_${r}_${a.slice(0,20)}`;return m?o.jsx("div",{style:d,children:o.jsx("hr",{className:"inkweave-divider"})},y):o.jsx("div",{style:d,children:o.jsx("p",{className:t,children:a})},y)})})},k=c.memo(f);class _{constructor(){C(this,"_components",new Map)}get(t){return this._components.get(t)}register(t,n){this._components.set(t,n)}unregister(t){this._components.delete(t)}clear(){this._components.clear()}has(t){return this._components.has(t)}}const b=new _,x=c.memo(({choice:e,index:t,onClick:n,className:l})=>{const s=c.useCallback(()=>{e.type!=="unclickable"&&n(e.index)},[e.index,e.type,n]),i=b.get(e.type);return i?o.jsx("li",{style:{"--index":t},children:c.createElement(i,{onClick:s,className:l,val:e.val,children:e.text})}):o.jsx("li",{style:{"--index":t},children:o.jsx("a",{onClick:s,className:`${l} ${e.type==="unclickable"?"disabled":""}`,"aria-disabled":e.type==="unclickable",children:e.text})})});x.displayName="ChoiceItem";const P=({onClick:e,className:t="inkweave-btn"})=>{const n=h(),l=u.choicesStore(r=>r.choices),s=n,i=s&&"choicesCanShow"in s?s.choicesCanShow:!0,a=c.useCallback(r=>{e==null||e(r),n.choose(r)},[n,e]);return o.jsx("ul",{id:"inkweave-choices",style:{visibility:i?"visible":"hidden"},children:l.map((r,d)=>o.jsx(x,{choice:r,index:d,onClick:a,className:t},r.index))},i.toString())},O=c.memo(P);Object.defineProperty(exports,"useChoices",{enumerable:!0,get:()=>u.choicesStore});Object.defineProperty(exports,"useContents",{enumerable:!0,get:()=>u.contentsStore});Object.defineProperty(exports,"useVariables",{enumerable:!0,get:()=>u.variablesStore});exports.ChoiceComponents=b;exports.Choices=O;exports.Contents=k;exports.Story=g;exports.StoryProvider=v;exports.useStory=h;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/components/Story.tsx","../src/components/Contents.tsx","../src/components/ChoiceComponents.ts","../src/components/Choices.tsx"],"sourcesContent":["import { memo, useEffect, useRef, createContext, useContext } from 'react';\nimport type { InkStory } from '@inkweave/core';\n\nconst StoryContext = createContext<InkStory | null>(null);\n\nexport const useStory = () => {\n\tconst ink = useContext(StoryContext);\n\tif (!ink) {\n\t\tthrow new Error('useStory must be used within StoryProvider');\n\t}\n\treturn ink;\n};\n\ninterface StoryProviderProps {\n\tink: InkStory;\n\tchildren: React.ReactNode;\n}\n\nexport const StoryProvider: React.FC<StoryProviderProps> = ({\n\tink,\n\tchildren,\n}) => {\n\treturn (\n\t\t<StoryContext.Provider value={ink}>\n\t\t\t{children}\n\t\t</StoryContext.Provider>\n\t);\n};\n\ninterface StoryProps {\n\tink: InkStory;\n\tchildren?: React.ReactNode;\n\tclassName?: string;\n\tonInit?: (ink: InkStory) => void;\n}\n\nconst StoryComponent: React.FC<StoryProps> = ({\n\tink,\n\tchildren,\n\tclassName = '',\n\tonInit,\n}) => {\n\tconst onInitRef = useRef(onInit);\n\tonInitRef.current = onInit;\n\n\tuseEffect(() => {\n\t\tink.restart();\n\t\tonInitRef.current?.(ink);\n\t}, [ink]);\n\n\treturn (\n\t\t<StoryProvider ink={ink}>\n\t\t\t<div id=\"inkweave-story\" className={className}>\n\t\t\t\t{children}\n\t\t\t</div>\n\t\t</StoryProvider>\n\t);\n};\n\nexport default memo(StoryComponent);","import { memo, CSSProperties } from 'react';\nimport { CHOICE_SEPARATOR, contentsStore } from '@inkweave/core';\nimport { useStory } from './Story';\n\ninterface ContentsProps {\n\tlineDelay?: number;\n\tclassName?: string;\n}\n\nconst ContentsComponent: React.FC<ContentsProps> = ({\n\tlineDelay = 0.05,\n\tclassName = '',\n}) => {\n\tconst ink = useStory();\n\tconst contents = contentsStore((state) => state.contents);\n\tconst inkRecord = ink as unknown as Record<string, unknown>;\n\tconst visibleLines =\n\t\tinkRecord?.visibleLines != undefined\n\t\t\t? (inkRecord.visibleLines as number)\n\t\t\t: contents.length;\n\n\treturn (\n\t\t<section id=\"inkweave-contents\">\n\t\t\t{contents.map((item: string, i: number) => {\n\t\t\t\tconst style: CSSProperties = {\n\t\t\t\t\t'--delay': `${\n\t\t\t\t\t\t(i > visibleLines ? i - visibleLines : 0) * lineDelay\n\t\t\t\t\t}s`,\n\t\t\t\t} as CSSProperties & { '--delay': string };\n\n\t\t\t\tconst isDivider = item === CHOICE_SEPARATOR;\n\t\t\t\tconst key = isDivider ? `divider_${i}` : `line_${i}_${item.slice(0, 20)}`;\n\n\t\t\t\tif (isDivider) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<div key={key} style={style}>\n\t\t\t\t\t\t\t<hr className=\"inkweave-divider\" />\n\t\t\t\t\t\t</div>\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn (\n\t\t\t\t\t<div key={key} style={style}>\n\t\t\t\t\t\t<p className={className}>{item}</p>\n\t\t\t\t\t</div>\n\t\t\t\t);\n\t\t\t})}\n\t\t</section>\n\t);\n};\n\nexport default memo(ContentsComponent);","import type { FC } from 'react';\n\nexport interface ChoiceComponentProps {\n\tval?: string;\n\tonClick: () => void;\n\tclassName?: string;\n\tchildren?: React.ReactNode;\n}\n\ntype ChoiceComponent = FC<ChoiceComponentProps>;\n\nclass ChoiceComponentsClass {\n\tprivate _components: Map<string, ChoiceComponent> = new Map();\n\n\tget(type: string): ChoiceComponent | undefined {\n\t\treturn this._components.get(type);\n\t}\n\n\tregister(type: string, component: ChoiceComponent) {\n\t\tthis._components.set(type, component);\n\t}\n\n\tunregister(type: string) {\n\t\tthis._components.delete(type);\n\t}\n\n\tclear() {\n\t\tthis._components.clear();\n\t}\n\n\thas(type: string): boolean {\n\t\treturn this._components.has(type);\n\t}\n}\n\nexport const ChoiceComponents = new ChoiceComponentsClass();","import { memo, createElement, useCallback } from 'react';\nimport { Choice, choicesStore } from '@inkweave/core';\nimport { useStory } from './Story';\nimport { ChoiceComponents } from './ChoiceComponents';\n\ninterface ChoiceItemProps {\n\tchoice: Choice;\n\tindex: number;\n\tonClick: (index: number) => void;\n\tclassName: string;\n}\n\nconst ChoiceItem: React.FC<ChoiceItemProps> = memo(({ choice, index, onClick, className }) => {\n\tconst handleClick = useCallback(() => {\n\t\tif (choice.type !== 'unclickable') {\n\t\t\tonClick(choice.index);\n\t\t}\n\t}, [choice.index, choice.type, onClick]);\n\n\tconst Component = ChoiceComponents.get(choice.type);\n\tif (Component) {\n\t\treturn (\n\t\t\t<li style={{ '--index': index } as React.CSSProperties}>\n\t\t\t\t{createElement(Component, {\n\t\t\t\t\tonClick: handleClick,\n\t\t\t\t\tclassName,\n\t\t\t\t\tval: choice.val,\n\t\t\t\t\tchildren: choice.text,\n\t\t\t\t})}\n\t\t\t</li>\n\t\t);\n\t}\n\n\treturn (\n\t\t<li style={{ '--index': index } as React.CSSProperties}>\n\t\t\t<a\n\t\t\t\tonClick={handleClick}\n\t\t\t\tclassName={`${className} ${choice.type === 'unclickable' ? 'disabled' : ''}`}\n\t\t\t\taria-disabled={choice.type === 'unclickable'}\n\t\t\t>\n\t\t\t\t{choice.text}\n\t\t\t</a>\n\t\t</li>\n\t);\n});\nChoiceItem.displayName = 'ChoiceItem';\n\ninterface ChoicesProps {\n\tonClick?: (index: number) => void;\n\tclassName?: string;\n}\n\nconst ChoicesComponent: React.FC<ChoicesProps> = ({\n\tonClick,\n\tclassName = 'inkweave-btn',\n}) => {\n\tconst ink = useStory();\n\tconst choices = choicesStore((state) => state.choices);\n\tconst inkRecord = ink as unknown as Record<string, unknown>;\n\tconst canShow =\n\t\tinkRecord && 'choicesCanShow' in inkRecord\n\t\t\t? (inkRecord.choicesCanShow as boolean)\n\t\t\t: true;\n\n\tconst handleClick = useCallback((index: number) => {\n\t\tonClick?.(index);\n\t\tink.choose(index);\n\t}, [ink, onClick]);\n\n\treturn (\n\t\t<ul\n\t\t\tid=\"inkweave-choices\"\n\t\t\tkey={canShow.toString()}\n\t\t\tstyle={{ visibility: canShow ? 'visible' : 'hidden' }}\n\t\t>\n\t\t\t{choices.map((choice: Choice, index: number) => (\n\t\t\t\t<ChoiceItem\n\t\t\t\t\tkey={choice.index}\n\t\t\t\t\tchoice={choice}\n\t\t\t\t\tindex={index}\n\t\t\t\t\tonClick={handleClick}\n\t\t\t\t\tclassName={className}\n\t\t\t\t/>\n\t\t\t))}\n\t\t</ul>\n\t);\n};\n\nexport default memo(ChoicesComponent);"],"names":["StoryContext","createContext","useStory","ink","useContext","StoryProvider","children","StoryComponent","className","onInit","onInitRef","useRef","useEffect","_a","jsx","Story","memo","ContentsComponent","lineDelay","contents","contentsStore","state","inkRecord","visibleLines","item","i","style","isDivider","CHOICE_SEPARATOR","key","Contents","ChoiceComponentsClass","__publicField","type","component","ChoiceComponents","ChoiceItem","choice","index","onClick","handleClick","useCallback","Component","ChoicesComponent","choices","choicesStore","canShow","Choices"],"mappings":"wUAGMA,EAAeC,EAAAA,cAA+B,IAAI,EAE3CC,EAAW,IAAM,CAC7B,MAAMC,EAAMC,EAAAA,WAAWJ,CAAY,EACnC,GAAI,CAACG,EACJ,MAAM,IAAI,MAAM,4CAA4C,EAE7D,OAAOA,CACR,EAOaE,EAA8C,CAAC,CAC3D,IAAAF,EACA,SAAAG,CACD,UAEGN,EAAa,SAAb,CAAsB,MAAOG,EAC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,OAAO,EAC/CC,EAAYnB,EACZ2C,EACLxB,GAAa,mBAAoBA,EAC7BA,EAAU,eACX,GAEEkB,EAAcC,cAAaH,GAAkB,CAClDC,GAAA,MAAAA,EAAUD,GACVnC,EAAI,OAAOmC,CAAK,CACjB,EAAG,CAACnC,EAAKoC,CAAO,CAAC,EAEjB,OACCzB,EAAAA,IAAC,KAAA,CACA,GAAG,mBAEH,MAAO,CAAE,WAAYgC,EAAU,UAAY,QAAA,EAE1C,SAAAF,EAAQ,IAAI,CAACP,EAAgBC,IAC7BxB,EAAAA,IAACsB,EAAA,CAEA,OAAAC,EACA,MAAAC,EACA,QAASE,EACT,UAAAhC,CAAA,EAJK6B,EAAO,KAAA,CAMb,CAAA,EAXIS,EAAQ,SAAA,CAAS,CAczB,EAEAC,EAAe/B,EAAAA,KAAK2B,CAAgB"}
@@ -0,0 +1,5 @@
1
+ export { default as Story, useStory, StoryProvider } from './components/Story';
2
+ export { default as Contents } from './components/Contents';
3
+ export { default as Choices } from './components/Choices';
4
+ export { ChoiceComponents, type ChoiceComponentProps } from './components/ChoiceComponents';
5
+ export { contentsStore as useContents, choicesStore as useChoices, variablesStore as useVariables } from '@inkweave/core';
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
1
+ var S = Object.defineProperty;
2
+ var b = (e, t, n) => t in e ? S(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var m = (e, t, n) => b(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { jsx as o } from "react/jsx-runtime";
5
+ import { createContext as w, memo as d, useRef as x, useEffect as f, useContext as _, useCallback as p, createElement as k } from "react";
6
+ import { contentsStore as g, CHOICE_SEPARATOR as $, choicesStore as E } from "@inkweave/core";
7
+ import { choicesStore as B, contentsStore as F, variablesStore as G } from "@inkweave/core";
8
+ const v = w(null), C = () => {
9
+ const e = _(v);
10
+ if (!e)
11
+ throw new Error("useStory must be used within StoryProvider");
12
+ return e;
13
+ }, P = ({
14
+ ink: e,
15
+ children: t
16
+ }) => /* @__PURE__ */ o(v.Provider, { value: e, children: t }), L = ({
17
+ ink: e,
18
+ children: t,
19
+ className: n = "",
20
+ onInit: c
21
+ }) => {
22
+ const s = x(c);
23
+ return s.current = c, f(() => {
24
+ var i;
25
+ e.restart(), (i = s.current) == null || i.call(s, e);
26
+ }, [e]), /* @__PURE__ */ o(P, { ink: e, children: /* @__PURE__ */ o("div", { id: "inkweave-story", className: n, children: t }) });
27
+ }, M = d(L), A = ({
28
+ lineDelay: e = 0.05,
29
+ className: t = ""
30
+ }) => {
31
+ const n = C(), c = g((l) => l.contents), s = n, i = (s == null ? void 0 : s.visibleLines) != null ? s.visibleLines : c.length;
32
+ return /* @__PURE__ */ o("section", { id: "inkweave-contents", children: c.map((l, r) => {
33
+ const a = {
34
+ "--delay": `${(r > i ? r - i : 0) * e}s`
35
+ }, h = l === $, u = h ? `divider_${r}` : `line_${r}_${l.slice(0, 20)}`;
36
+ return h ? /* @__PURE__ */ o("div", { style: a, children: /* @__PURE__ */ o("hr", { className: "inkweave-divider" }) }, u) : /* @__PURE__ */ o("div", { style: a, children: /* @__PURE__ */ o("p", { className: t, children: l }) }, u);
37
+ }) });
38
+ }, T = d(A);
39
+ class N {
40
+ constructor() {
41
+ m(this, "_components", /* @__PURE__ */ new Map());
42
+ }
43
+ get(t) {
44
+ return this._components.get(t);
45
+ }
46
+ register(t, n) {
47
+ this._components.set(t, n);
48
+ }
49
+ unregister(t) {
50
+ this._components.delete(t);
51
+ }
52
+ clear() {
53
+ this._components.clear();
54
+ }
55
+ has(t) {
56
+ return this._components.has(t);
57
+ }
58
+ }
59
+ const O = new N(), y = d(({ choice: e, index: t, onClick: n, className: c }) => {
60
+ const s = p(() => {
61
+ e.type !== "unclickable" && n(e.index);
62
+ }, [e.index, e.type, n]), i = O.get(e.type);
63
+ return i ? /* @__PURE__ */ o("li", { style: { "--index": t }, children: k(i, {
64
+ onClick: s,
65
+ className: c,
66
+ val: e.val,
67
+ children: e.text
68
+ }) }) : /* @__PURE__ */ o("li", { style: { "--index": t }, children: /* @__PURE__ */ o(
69
+ "a",
70
+ {
71
+ onClick: s,
72
+ className: `${c} ${e.type === "unclickable" ? "disabled" : ""}`,
73
+ "aria-disabled": e.type === "unclickable",
74
+ children: e.text
75
+ }
76
+ ) });
77
+ });
78
+ y.displayName = "ChoiceItem";
79
+ const R = ({
80
+ onClick: e,
81
+ className: t = "inkweave-btn"
82
+ }) => {
83
+ const n = C(), c = E((r) => r.choices), s = n, i = s && "choicesCanShow" in s ? s.choicesCanShow : !0, l = p((r) => {
84
+ e == null || e(r), n.choose(r);
85
+ }, [n, e]);
86
+ return /* @__PURE__ */ o(
87
+ "ul",
88
+ {
89
+ id: "inkweave-choices",
90
+ style: { visibility: i ? "visible" : "hidden" },
91
+ children: c.map((r, a) => /* @__PURE__ */ o(
92
+ y,
93
+ {
94
+ choice: r,
95
+ index: a,
96
+ onClick: l,
97
+ className: t
98
+ },
99
+ r.index
100
+ ))
101
+ },
102
+ i.toString()
103
+ );
104
+ }, V = d(R);
105
+ export {
106
+ O as ChoiceComponents,
107
+ V as Choices,
108
+ T as Contents,
109
+ M as Story,
110
+ P as StoryProvider,
111
+ B as useChoices,
112
+ F as useContents,
113
+ C as useStory,
114
+ G as useVariables
115
+ };
116
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/components/Story.tsx","../src/components/Contents.tsx","../src/components/ChoiceComponents.ts","../src/components/Choices.tsx"],"sourcesContent":["import { memo, useEffect, useRef, createContext, useContext } from 'react';\nimport type { InkStory } from '@inkweave/core';\n\nconst StoryContext = createContext<InkStory | null>(null);\n\nexport const useStory = () => {\n\tconst ink = useContext(StoryContext);\n\tif (!ink) {\n\t\tthrow new Error('useStory must be used within StoryProvider');\n\t}\n\treturn ink;\n};\n\ninterface StoryProviderProps {\n\tink: InkStory;\n\tchildren: React.ReactNode;\n}\n\nexport const StoryProvider: React.FC<StoryProviderProps> = ({\n\tink,\n\tchildren,\n}) => {\n\treturn (\n\t\t<StoryContext.Provider value={ink}>\n\t\t\t{children}\n\t\t</StoryContext.Provider>\n\t);\n};\n\ninterface StoryProps {\n\tink: InkStory;\n\tchildren?: React.ReactNode;\n\tclassName?: string;\n\tonInit?: (ink: InkStory) => void;\n}\n\nconst StoryComponent: React.FC<StoryProps> = ({\n\tink,\n\tchildren,\n\tclassName = '',\n\tonInit,\n}) => {\n\tconst onInitRef = useRef(onInit);\n\tonInitRef.current = onInit;\n\n\tuseEffect(() => {\n\t\tink.restart();\n\t\tonInitRef.current?.(ink);\n\t}, [ink]);\n\n\treturn (\n\t\t<StoryProvider ink={ink}>\n\t\t\t<div id=\"inkweave-story\" className={className}>\n\t\t\t\t{children}\n\t\t\t</div>\n\t\t</StoryProvider>\n\t);\n};\n\nexport default memo(StoryComponent);","import { memo, CSSProperties } from 'react';\nimport { CHOICE_SEPARATOR, contentsStore } from '@inkweave/core';\nimport { useStory } from './Story';\n\ninterface ContentsProps {\n\tlineDelay?: number;\n\tclassName?: string;\n}\n\nconst ContentsComponent: React.FC<ContentsProps> = ({\n\tlineDelay = 0.05,\n\tclassName = '',\n}) => {\n\tconst ink = useStory();\n\tconst contents = contentsStore((state) => state.contents);\n\tconst inkRecord = ink as unknown as Record<string, unknown>;\n\tconst visibleLines =\n\t\tinkRecord?.visibleLines != undefined\n\t\t\t? (inkRecord.visibleLines as number)\n\t\t\t: contents.length;\n\n\treturn (\n\t\t<section id=\"inkweave-contents\">\n\t\t\t{contents.map((item: string, i: number) => {\n\t\t\t\tconst style: CSSProperties = {\n\t\t\t\t\t'--delay': `${\n\t\t\t\t\t\t(i > visibleLines ? i - visibleLines : 0) * lineDelay\n\t\t\t\t\t}s`,\n\t\t\t\t} as CSSProperties & { '--delay': string };\n\n\t\t\t\tconst isDivider = item === CHOICE_SEPARATOR;\n\t\t\t\tconst key = isDivider ? `divider_${i}` : `line_${i}_${item.slice(0, 20)}`;\n\n\t\t\t\tif (isDivider) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<div key={key} style={style}>\n\t\t\t\t\t\t\t<hr className=\"inkweave-divider\" />\n\t\t\t\t\t\t</div>\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn (\n\t\t\t\t\t<div key={key} style={style}>\n\t\t\t\t\t\t<p className={className}>{item}</p>\n\t\t\t\t\t</div>\n\t\t\t\t);\n\t\t\t})}\n\t\t</section>\n\t);\n};\n\nexport default memo(ContentsComponent);","import type { FC } from 'react';\n\nexport interface ChoiceComponentProps {\n\tval?: string;\n\tonClick: () => void;\n\tclassName?: string;\n\tchildren?: React.ReactNode;\n}\n\ntype ChoiceComponent = FC<ChoiceComponentProps>;\n\nclass ChoiceComponentsClass {\n\tprivate _components: Map<string, ChoiceComponent> = new Map();\n\n\tget(type: string): ChoiceComponent | undefined {\n\t\treturn this._components.get(type);\n\t}\n\n\tregister(type: string, component: ChoiceComponent) {\n\t\tthis._components.set(type, component);\n\t}\n\n\tunregister(type: string) {\n\t\tthis._components.delete(type);\n\t}\n\n\tclear() {\n\t\tthis._components.clear();\n\t}\n\n\thas(type: string): boolean {\n\t\treturn this._components.has(type);\n\t}\n}\n\nexport const ChoiceComponents = new ChoiceComponentsClass();","import { memo, createElement, useCallback } from 'react';\nimport { Choice, choicesStore } from '@inkweave/core';\nimport { useStory } from './Story';\nimport { ChoiceComponents } from './ChoiceComponents';\n\ninterface ChoiceItemProps {\n\tchoice: Choice;\n\tindex: number;\n\tonClick: (index: number) => void;\n\tclassName: string;\n}\n\nconst ChoiceItem: React.FC<ChoiceItemProps> = memo(({ choice, index, onClick, className }) => {\n\tconst handleClick = useCallback(() => {\n\t\tif (choice.type !== 'unclickable') {\n\t\t\tonClick(choice.index);\n\t\t}\n\t}, [choice.index, choice.type, onClick]);\n\n\tconst Component = ChoiceComponents.get(choice.type);\n\tif (Component) {\n\t\treturn (\n\t\t\t<li style={{ '--index': index } as React.CSSProperties}>\n\t\t\t\t{createElement(Component, {\n\t\t\t\t\tonClick: handleClick,\n\t\t\t\t\tclassName,\n\t\t\t\t\tval: choice.val,\n\t\t\t\t\tchildren: choice.text,\n\t\t\t\t})}\n\t\t\t</li>\n\t\t);\n\t}\n\n\treturn (\n\t\t<li style={{ '--index': index } as React.CSSProperties}>\n\t\t\t<a\n\t\t\t\tonClick={handleClick}\n\t\t\t\tclassName={`${className} ${choice.type === 'unclickable' ? 'disabled' : ''}`}\n\t\t\t\taria-disabled={choice.type === 'unclickable'}\n\t\t\t>\n\t\t\t\t{choice.text}\n\t\t\t</a>\n\t\t</li>\n\t);\n});\nChoiceItem.displayName = 'ChoiceItem';\n\ninterface ChoicesProps {\n\tonClick?: (index: number) => void;\n\tclassName?: string;\n}\n\nconst ChoicesComponent: React.FC<ChoicesProps> = ({\n\tonClick,\n\tclassName = 'inkweave-btn',\n}) => {\n\tconst ink = useStory();\n\tconst choices = choicesStore((state) => state.choices);\n\tconst inkRecord = ink as unknown as Record<string, unknown>;\n\tconst canShow =\n\t\tinkRecord && 'choicesCanShow' in inkRecord\n\t\t\t? (inkRecord.choicesCanShow as boolean)\n\t\t\t: true;\n\n\tconst handleClick = useCallback((index: number) => {\n\t\tonClick?.(index);\n\t\tink.choose(index);\n\t}, [ink, onClick]);\n\n\treturn (\n\t\t<ul\n\t\t\tid=\"inkweave-choices\"\n\t\t\tkey={canShow.toString()}\n\t\t\tstyle={{ visibility: canShow ? 'visible' : 'hidden' }}\n\t\t>\n\t\t\t{choices.map((choice: Choice, index: number) => (\n\t\t\t\t<ChoiceItem\n\t\t\t\t\tkey={choice.index}\n\t\t\t\t\tchoice={choice}\n\t\t\t\t\tindex={index}\n\t\t\t\t\tonClick={handleClick}\n\t\t\t\t\tclassName={className}\n\t\t\t\t/>\n\t\t\t))}\n\t\t</ul>\n\t);\n};\n\nexport default memo(ChoicesComponent);"],"names":["StoryContext","createContext","useStory","ink","useContext","StoryProvider","children","StoryComponent","className","onInit","onInitRef","useRef","useEffect","_a","jsx","Story","memo","ContentsComponent","lineDelay","contents","contentsStore","state","inkRecord","visibleLines","item","i","style","isDivider","CHOICE_SEPARATOR","key","Contents","ChoiceComponentsClass","__publicField","type","component","ChoiceComponents","ChoiceItem","choice","index","onClick","handleClick","useCallback","Component","ChoicesComponent","choices","choicesStore","canShow","Choices"],"mappings":";;;;;;;AAGA,MAAMA,IAAeC,EAA+B,IAAI,GAE3CC,IAAW,MAAM;AAC7B,QAAMC,IAAMC,EAAWJ,CAAY;AACnC,MAAI,CAACG;AACJ,UAAM,IAAI,MAAM,4CAA4C;AAE7D,SAAOA;AACR,GAOaE,IAA8C,CAAC;AAAA,EAC3D,KAAAF;AAAA,EACA,UAAAG;AACD,wBAEGN,EAAa,UAAb,EAAsB,OAAOG,GAC5B,UAAAG,GACF,GAWIC,IAAuC,CAAC;AAAA,EAC7C,KAAAJ;AAAA,EACA,UAAAG;AAAA,EACA,WAAAE,IAAY;AAAA,EACZ,QAAAC;AACD,MAAM;AACL,QAAMC,IAAYC,EAAOF,CAAM;AAC/B,SAAAC,EAAU,UAAUD,GAEpBG,EAAU,MAAM;;AACf,IAAAT,EAAI,QAAA,IACJU,IAAAH,EAAU,YAAV,QAAAG,EAAA,KAAAH,GAAoBP;AAAA,EACrB,GAAG,CAACA,CAAG,CAAC,GAGP,gBAAAW,EAACT,KAAc,KAAAF,GACd,UAAA,gBAAAW,EAAC,SAAI,IAAG,kBAAiB,WAAAN,GACvB,UAAAF,EAAA,CACF,EAAA,CACD;AAEF,GAEAS,IAAeC,EAAKT,CAAc,GClD5BU,IAA6C,CAAC;AAAA,EACnD,WAAAC,IAAY;AAAA,EACZ,WAAAV,IAAY;AACb,MAAM;AACL,QAAML,IAAMD,EAAA,GACNiB,IAAWC,EAAc,CAACC,MAAUA,EAAM,QAAQ,GAClDC,IAAYnB,GACZoB,KACLD,KAAA,gBAAAA,EAAW,iBAAgB,OACvBA,EAAU,eACXH,EAAS;AAEb,SACC,gBAAAL,EAAC,aAAQ,IAAG,qBACV,YAAS,IAAI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IAAA;AAAA,IAXIS,EAAQ,SAAA;AAAA,EAAS;AAczB,GAEAC,IAAe/B,EAAK2B,CAAgB;"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@inkweave/react",
3
+ "version": "1.0.0",
4
+ "description": "React components for InkWeave",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc --noEmit && vite build",
21
+ "typecheck": "tsc --noEmit"
22
+ },
23
+ "keywords": [
24
+ "ink",
25
+ "interactive-fiction",
26
+ "react",
27
+ "inkweave"
28
+ ],
29
+ "author": "Uglyboy",
30
+ "license": "MIT",
31
+ "peerDependencies": {
32
+ "@inkweave/core": "^1.0.0",
33
+ "react": ">=18.0.0",
34
+ "react-dom": ">=18.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/react": "^19.0.0",
38
+ "@types/react-dom": "^19.0.0",
39
+ "@vitejs/plugin-react": "^4.4.0",
40
+ "react": "^19.0.0",
41
+ "react-dom": "^19.0.0",
42
+ "typescript": "^5.0.0",
43
+ "vite": "^6.0.0",
44
+ "vite-plugin-dts": "^4.5.4"
45
+ }
46
+ }