UNPKG

flexium

Version:

A lightweight, signals-based UI framework with cross-platform renderers

1 lines 6.41 kB
{"version":3,"sources":["../src/core/context.ts","../src/renderers/dom/h.ts"],"names":["contextStacks","createContext","defaultValue","id","Provider","props","children","useContext","context","stack","pushProvider","value","popProvider","h","type","normalizedProps","key","normalizedChildren","flattenChildren","child","createVNode","Fragment","result","isVNode","createTextVNode","text"],"mappings":"4CAiBA,IAAMA,CAAAA,CAAgB,IAAI,GAAA,CAQnB,SAASC,EAAiBC,CAAAA,CAA6B,CAC1D,IAAMC,CAAAA,CAAK,MAAA,CAAO,SAAS,EAErBC,CAAAA,CAAYC,CAAAA,EAAuC,CACrD,IAAMC,CAAAA,CAAWD,CAAAA,CAAM,SACvB,OAAI,KAAA,CAAM,OAAA,CAAQC,CAAQ,CAAA,EAAKA,CAAAA,CAAS,SAAW,CAAA,CACxCA,CAAAA,CAAS,CAAC,CAAA,CAEdA,CACX,EAGA,OAACF,CAAAA,CAAiB,UAAA,CAAaD,CAAAA,CAExB,CACH,EAAA,CAAAA,EACA,YAAA,CAAAD,CAAAA,CACA,QAAA,CAAAE,CACJ,CACJ,CAQO,SAASG,CAAAA,CAAcC,CAAAA,CAAwB,CAClD,IAAMC,CAAAA,CAAQT,CAAAA,CAAc,IAAIQ,CAAAA,CAAQ,EAAE,EAC1C,OAAIC,CAAAA,EAASA,EAAM,MAAA,CAAS,CAAA,CACjBA,CAAAA,CAAMA,CAAAA,CAAM,MAAA,CAAS,CAAC,EAE1BD,CAAAA,CAAQ,YACnB,CAMO,SAASE,CAAAA,CAAaP,CAAAA,CAAYQ,EAAY,CAC5CX,CAAAA,CAAc,GAAA,CAAIG,CAAE,CAAA,EACrBH,CAAAA,CAAc,IAAIG,CAAAA,CAAI,EAAE,CAAA,CAE5BH,CAAAA,CAAc,GAAA,CAAIG,CAAE,CAAA,CAAG,IAAA,CAAKQ,CAAK,EACrC,CAMO,SAASC,EAAYT,CAAAA,CAAY,CACpC,IAAMM,CAAAA,CAAQT,CAAAA,CAAc,GAAA,CAAIG,CAAE,CAAA,CAC9BM,CAAAA,EACAA,CAAAA,CAAM,GAAA,GAEd,CCpDO,SAASI,CAAAA,CACdC,CAAAA,CACAT,KACGC,CAAAA,CACI,CAEP,IAAMS,CAAAA,CAAkBV,CAAAA,EAAS,EAAC,CAG5BW,CAAAA,CAAMD,CAAAA,CAAgB,IACxBC,CAAAA,GAAQ,MAAA,EACV,OAAOD,CAAAA,CAAgB,GAAA,CAIzB,IAAME,EAAqBC,CAAAA,CAAgBZ,CAAQ,CAAA,CAAE,MAAA,CAClDa,CAAAA,EAAUA,CAAAA,EAAU,MAA+BA,CAAAA,GAAU,KAChE,EAEA,OAAOC,GAAAA,CAAYN,EAAMC,CAAAA,CAAiBE,CAAAA,CAAoBD,CAAG,CACnE,CAMO,SAASK,EAAShB,CAAAA,CAAoC,CAC3D,OAAOe,GAAAA,CAAY,UAAA,CAAY,GAAIf,CAAAA,CAAM,QAAA,EAAY,EAAE,CACzD,CAKA,SAASa,CAAAA,CAAgBZ,CAAAA,CAAwB,CAC/C,IAAMgB,CAAAA,CAAgB,EAAC,CAEvB,IAAA,IAAWH,CAAAA,IAASb,CAAAA,CACd,KAAA,CAAM,OAAA,CAAQa,CAAK,CAAA,CACrBG,CAAAA,CAAO,IAAA,CAAK,GAAGJ,CAAAA,CAAgBC,CAAK,CAAC,CAAA,CAErCG,CAAAA,CAAO,IAAA,CAAKH,CAAK,CAAA,CAIrB,OAAOG,CACT,CAKO,SAASC,CAAAA,CAAQZ,CAAAA,CAA4B,CAClD,OACEA,IAAU,IAAA,EACV,OAAOA,CAAAA,EAAU,QAAA,EACjB,MAAA,GAAUA,CAAAA,EACV,UAAWA,CAAAA,EACX,UAAA,GAAcA,CAElB,CAKO,SAASa,CAAAA,CAAgBC,EAA+B,CAC7D,OAAO,MAAA,CAAOA,CAAI,CACpB","file":"chunk-H3V4FX7A.mjs","sourcesContent":["/**\n * Context API for dependency injection\n */\n\n/**\n * Context interface\n */\nexport interface Context<T> {\n id: symbol;\n defaultValue: T;\n Provider: (props: { value: T; children: any }) => any;\n}\n\n/**\n * Internal map to track current context values\n * Key is the context symbol, value is the stack of values\n */\nconst contextStacks = new Map<symbol, any[]>();\n\n/**\n * Creates a Context object\n *\n * @param defaultValue - The default value if no provider is found\n * @returns Context object with Provider component\n */\nexport function createContext<T>(defaultValue: T): Context<T> {\n const id = Symbol('context');\n\n const Provider = (props: { value: T; children: any }) => {\n const children = props.children;\n if (Array.isArray(children) && children.length === 1) {\n return children[0];\n }\n return children;\n };\n\n // Mark as a context provider for the renderer\n (Provider as any)._contextId = id;\n\n return {\n id,\n defaultValue,\n Provider\n };\n}\n\n/**\n * Retrieves the current value of a Context\n *\n * @param context - The context object\n * @returns The current value\n */\nexport function useContext<T>(context: Context<T>): T {\n const stack = contextStacks.get(context.id);\n if (stack && stack.length > 0) {\n return stack[stack.length - 1];\n }\n return context.defaultValue;\n}\n\n/**\n * Internal: Push a provider value onto the stack\n * @internal\n */\nexport function pushProvider(id: symbol, value: any) {\n if (!contextStacks.has(id)) {\n contextStacks.set(id, []);\n }\n contextStacks.get(id)!.push(value);\n}\n\n/**\n * Internal: Pop a provider value from the stack\n * @internal\n */\nexport function popProvider(id: symbol) {\n const stack = contextStacks.get(id);\n if (stack) {\n stack.pop();\n }\n}\n","/**\n * JSX Factory Function (h)\n *\n * This module provides the JSX factory function for creating virtual nodes.\n * It's used by the JSX transpiler to convert JSX syntax into function calls.\n *\n * Usage in tsconfig.json:\n * {\n * \"compilerOptions\": {\n * \"jsx\": \"react\",\n * \"jsxFactory\": \"h\",\n * \"jsxFragmentFactory\": \"Fragment\"\n * }\n * }\n */\n\nimport type { VNode } from '../../core/renderer';\nimport { createVNode } from '../../core/vnode';\n\n/**\n * JSX factory function\n * Creates a virtual node from JSX syntax\n *\n * @param type - Element type (string for built-in, function for components)\n * @param props - Element properties\n * @param children - Child elements\n * @returns Virtual node\n */\nexport function h(\n type: string | Function,\n props: Record<string, any> | null,\n ...children: any[]\n): VNode {\n // Normalize props\n const normalizedProps = props || {};\n\n // Extract key if present\n const key = normalizedProps.key;\n if (key !== undefined) {\n delete normalizedProps.key;\n }\n\n // Flatten and filter children\n const normalizedChildren = flattenChildren(children).filter(\n (child) => child !== null && child !== undefined && child !== false\n );\n\n return createVNode(type, normalizedProps, normalizedChildren, key);\n}\n\n/**\n * Fragment component\n * Renders children without a wrapper element\n */\nexport function Fragment(props: { children?: any[] }): VNode {\n return createVNode('fragment', {}, props.children || []);\n}\n\n/**\n * Flatten nested children arrays\n */\nfunction flattenChildren(children: any[]): any[] {\n const result: any[] = [];\n\n for (const child of children) {\n if (Array.isArray(child)) {\n result.push(...flattenChildren(child));\n } else {\n result.push(child);\n }\n }\n\n return result;\n}\n\n/**\n * Check if a value is a VNode\n */\nexport function isVNode(value: any): value is VNode {\n return (\n value !== null &&\n typeof value === 'object' &&\n 'type' in value &&\n 'props' in value &&\n 'children' in value\n );\n}\n\n/**\n * Create a text node\n */\nexport function createTextVNode(text: string | number): string {\n return String(text);\n}\n"]}