Skip to content

feat: Add Vue setup and showcase support #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
7 changes: 6 additions & 1 deletion js/showcase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"types": "./dist/src/react/index.d.ts",
"default": "./dist/react/index.js"
},
"./vue": {
"types": "./dist/src/vue/index.d.ts",
"default": "./dist/vue/index.js"
},
"./styles.css": {
"default": "./dist/styles.css"
}
Expand All @@ -32,7 +36,8 @@
},
"peerDependencies": {
"react": "^19.1.0",
"react-dom": "^19.1.0"
"react-dom": "^19.1.0",
"vue": "^3.5.17"
},
"devDependencies": {
"@eslint/js": "^9.28.0",
Expand Down
12 changes: 10 additions & 2 deletions js/showcase/src/case.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type PropDef = {
disabled?: boolean;
hidden?: boolean;
optional?: boolean;
isSlot?: boolean;
} & (
| {
kind: "raw";
Expand All @@ -39,7 +40,7 @@ export type PropDef = {
}
| {
kind: "icon";
default?: boolean;
default?: any;
options?: never[];
}
| {
Expand All @@ -58,6 +59,11 @@ export type PropKind = PropDef["kind"];

export interface CaseDef<TComponent> {
props: Record<string, Exclude<PropKind, "raw"> | PropDef>;
slots?: Record<
string,
| Exclude<PropKind, "raw" | "function" | "callback">
| Exclude<PropDef, { kind: "function" | "callback" }>
>;
component: TComponent;
}

Expand Down Expand Up @@ -167,8 +173,9 @@ function ShowCaseDef<TComponent, TNode>(
const {
defs: propDefs,
componentProps,
componentSlots,
componentEvents,
} = prepareProps(def.props, showcaseDef);
} = prepareProps(def.props, def.slots, showcaseDef);

const inputs = (
<div
Expand All @@ -186,6 +193,7 @@ function ShowCaseDef<TComponent, TNode>(
inputs: showcaseDef.attach(inputs),
component: def.component,
props: componentProps,
slots: componentSlots,
events: componentEvents,
});
}
42 changes: 33 additions & 9 deletions js/showcase/src/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,65 @@ const PROP_KIND_DEFAULTS: { [K in PropKind]: any } = {
function: () => {},
};

type NormalizedProps = Required<PropDef> & { id: string };

function normalizeProp(
propName: string,
prop: CaseDef<unknown>["props"][string]
): Required<PropDef> {
prop: CaseDef<unknown>["props"][string],
isSlot = false
): NormalizedProps {
if (typeof prop === "string") {
return {
id: propName,
displayName: propName,
kind: prop,
disabled: false,
hidden: false,
isSlot,
options: [],
default: PROP_KIND_DEFAULTS[prop],
optional: true,
} satisfies Required<PropDef>;
} satisfies NormalizedProps;
}

return {
id: propName,
default: prop.optional ? undefined : PROP_KIND_DEFAULTS[prop.kind],
displayName: propName,
disabled: false,
hidden: false,
isSlot,
optional: true,
options: [],
...prop,
} satisfies Required<PropDef>;
} satisfies NormalizedProps;
}

export function normalizeProps(
props: CaseDef<unknown>["props"]
): Required<PropDef>[] {
props: CaseDef<unknown>["props"],
isSlot = false
): NormalizedProps[] {
return Object.entries(props).map(([propName, propDef]) =>
normalizeProp(propName, propDef)
normalizeProp(propName, propDef, isSlot)
);
}

export function prepareProps(
props: CaseDef<unknown>["props"],
slots: CaseDef<unknown>["slots"],
showcaseDef: ShowcaseDef<unknown, unknown>
): {
defs: ShowcaseFieldProps[];
componentProps: Record<string, MiniUI.Signal<unknown>>;
componentSlots: Record<string, MiniUI.Signal<unknown>>;
componentEvents: Record<string, MiniUI.Signal<void>>;
} {
const normalizedProps = normalizeProps(props);
const normalizedSlots = normalizeProps(slots ?? {}, /* isSlot */ true);

const defs: ShowcaseFieldProps[] = [];
const componentProps: [string, MiniUI.Signal<unknown>][] = [];
const componentSlots: [string, MiniUI.Signal<unknown>][] = [];
const componentEvents: [string, MiniUI.Signal<void>][] = [];

for (const propDef of normalizedProps) {
Expand All @@ -78,7 +90,7 @@ export function prepareProps(
showcaseDef,
});
componentEvents.push([
propDef.displayName,
propDef.id,
() => {
valueSignal(true);
},
Expand All @@ -91,13 +103,25 @@ export function prepareProps(
valueSignal,
showcaseDef,
});
componentProps.push([propDef.displayName, valueSignal]);
componentProps.push([propDef.id, valueSignal]);
}
}

for (const slotDef of normalizedSlots) {
const valueSignal = createSignal(slotDef.default);

defs.push({
...slotDef,
valueSignal,
showcaseDef,
});
componentSlots.push([slotDef.id, valueSignal]);
}

return {
defs,
componentProps: Object.fromEntries(componentProps),
componentSlots: Object.fromEntries(componentSlots),
componentEvents: Object.fromEntries(componentEvents),
};
}
Expand Down
7 changes: 1 addition & 6 deletions js/showcase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ export interface ShowcaseDef<TComponent, TNode>
instiate(node: TComponent, props: unknown): TNode;
render(node: TComponent): Node;
renderNode(node: TNode): Node;
renderCaseSplitted(props: {
inputs: TNode;
component: TComponent;
props: Record<string, MiniUI.Signal<unknown>>;
events: Record<string, MiniUI.Signal<void>>;
}): TNode;
renderCaseSplitted: TComponent;
attach(node: MiniUI.Node): TNode;
createErrorBoundary(
render: () => TNode,
Expand Down
2 changes: 1 addition & 1 deletion js/showcase/src/miniui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function createEffect(fn: () => void): () => void {
return effect(fn);
}

export function renderH(parent: HTMLElement, node: MiniUI.Node) {
export function renderH(parent: Element, node: MiniUI.Node) {
parent.innerHTML = "";
appendChildren(parent, [node]);
}
Expand Down
2 changes: 1 addition & 1 deletion js/showcase/src/react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function setupShowcase(
])
);

createShowcase<(props: unknown) => React.ReactNode, React.ReactNode>({
createShowcase<(props: any) => React.ReactNode, React.ReactNode>({
...config,
icons,
instiate,
Expand Down
109 changes: 109 additions & 0 deletions js/showcase/src/vue/ErrorBoundary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
ComponentPublicInstance,
defineComponent,
h as vueH,
VNode,
} from "vue";
import { ErrorsDef, ErrorStack } from "../error";

export function createErrorBoundary(
render: () => VNode,
renderErrors: (error: ErrorsDef) => VNode
): VNode {
return vueH(ErrorBoundary, { render, renderErrors });
}

type ErrorBoundary = ComponentPublicInstance<{}, {}, ErrorBoundaryData>;
type ErrorBoundaryData = {
hasError: boolean;
errorsDef: ErrorsDef | null;
};
const ErrorBoundary = defineComponent<{
render: () => VNode;
renderErrors: (error: ErrorsDef) => VNode;
}>({
name: "ErrorBoundary",
props: ["render", "renderErrors"],
data: () =>
({
hasError: false,
errorsDef: null as ErrorsDef | null,
}) as ErrorBoundaryData,
setup({ render, renderErrors }) {
return (ctx: ErrorBoundary) =>
ctx.$data.hasError && ctx.$data.errorsDef != null
? renderErrors(ctx.$data.errorsDef)
: render();
},
errorCaptured(this: ErrorBoundary, err) {
console.log("ERROR CAPTURED", err);
this.$data.hasError = true;
this.$data.errorsDef = errorToDef(err as Error);
return false;
},
});

function errorToDef(error: Error): ErrorsDef {
const stack: ErrorStack[] = (error.stack?.split?.("\n") ?? [])
.map(line => {
if (line.length === 0) {
return;
}

// split by really last @ or @http(s)?://
let [, name, source = ""] = line.match(/(.*)@(https?:\/\/.*)$/) ??
line.match(/(.*)@(:?[^@]*)$/) ?? [, line];

// replace empty string to anonymous call
name = name || "<anonymous>";

// Ignore all the internal functions of react and vite
if (
source.includes("vite/client") ||
name.includes("/node_modules/") ||
name.startsWith("__require")
) {
return;
}

// Remove any URL prefix, leave just path
source = source.startsWith(location.origin)
? source.substring(location.origin.length)
: source;

// Remove node_modules prefix from URL
const NODE_MODULES_DEPS = "/node_modules/.vite/deps/";
source = source.startsWith(NODE_MODULES_DEPS)
? "/node_modules/" +
source.substring(NODE_MODULES_DEPS.length).replace(/_/g, "/")
: source;

const SHOWCASE_DIST = "showcase/dist";
source = source.includes(SHOWCASE_DIST)
? "@showcase" +
source.substring(source.indexOf(SHOWCASE_DIST) + SHOWCASE_DIST.length)
: source;

// match to <source-file>?<timestamp-or-version>:<line>:<column>
const [, sourceFile, lineN, columnN] = source.match(
/^(.+)?(?:t=\d+|v=\w+):(\d+):(\d+)$/
) ?? [, source, "", ""];

if (sourceFile.endsWith("vue.js?")) {
return;
}

return {
line: parseInt(lineN),
column: parseInt(columnN),
name: name,
source: sourceFile,
} satisfies ErrorStack;
})
.filter((e): e is ErrorStack => !!e);

return {
message: error.message,
stack,
};
}
62 changes: 62 additions & 0 deletions js/showcase/src/vue/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
ConcreteComponent,
createApp,
defineComponent,
VNode,
h as vueH,
} from "vue";
import { createShowcase, registerCase, ShowcaseConfigDef } from "..";
import { createErrorBoundary } from "./ErrorBoundary";
import {
attach,
instiate,
render,
renderCaseSplitted,
renderNode,
} from "./render";

export function setupShowcase(
config: ShowcaseConfigDef<ConcreteComponent<any>> & {
showcases: Record<string, ConcreteComponent<any>>;
}
) {
const virtualElem = document.createElement("div");
Object.values(config.showcases).forEach(comp => {
createApp(comp).mount(virtualElem);
});

const icons = Object.fromEntries(
Object.entries(config?.icons ?? {}).map(([iconName, iconComp]) => [
iconName,
vueH(iconComp),
])
);

createShowcase<ConcreteComponent<any>, VNode>({
...config,
icons,
instiate,
render,
renderNode,
renderCaseSplitted,
attach,
createErrorBoundary,
});
}

export default defineComponent({
name: "Showcase",
props: ["name", "props", "slots", "component"],
setup(props, { slots }) {
if (slots.default) {
registerCase(props.name, () => slots.default!());
} else {
registerCase(props.name, {
props: props.props,
slots: props.slots,
component: props.component,
});
}
return () => vueH("div");
},
});
Loading