Skip to content

Commit

Permalink
feat: add examples to the registry and update ComponentPreview
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-ub committed Aug 23, 2024
1 parent 5fbd7f9 commit daa2073
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
30 changes: 30 additions & 0 deletions apps/www/src/__registry__/examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { styles } from "@/registry/styles"

export const examples: Record<string, Record<string, any>> = {};

const components = import.meta.glob(`../registry/**/example/*.ts`);

for (const path in components) {
const component = components[path];
const name = path.split('/')[2];
const componentName = path.split('/').pop()?.replace('.ts', '');
if (!componentName || !name) {
continue;
}

if (!examples[name]) {
examples[name] = {};
}

examples[name][componentName] = {
name: componentName,
type: "components:example",
registryDependencies: [name],
component,
source: "",
files: [path],
category: "undefined",
subcategory: "undefined",
chunks: []
};
}
4 changes: 1 addition & 3 deletions apps/www/src/components/ComponentPreview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ const components = await Promise.all(
<SelectStyle syncKey={syncKey}>
{
components.map(({ style, path }) => (
<SelectStyleItem label={style.label}>
{ path ? <ComponentPeviewComponent {path} client:load /> : <p>Not found</p> }
</SelectStyleItem>
<SelectStyleItem label={style.label}>{path ? <ComponentPeviewComponent nameExample={name} styleName={style.name} client:load /> : <p>Not found</p>}</SelectStyleItem>
))
}
</SelectStyle>
Expand Down
20 changes: 16 additions & 4 deletions apps/www/src/components/component-preview.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { Component, computed, input } from '@angular/core';
import { AsyncPipe, NgComponentOutlet } from '@angular/common';

import { examples } from '@/__registry__/examples'

@Component({
standalone: true,
imports: [NgComponentOutlet, AsyncPipe],
template: `
<ng-container *ngComponentOutlet="component() | async" />
@let componentRender = this.component() | async;
@if(!componentRender || !componentRender.default) {
<div>Loading...</div>
} @else {
<ng-container *ngComponentOutlet="componentRender.default" />
}
`
})
export class ComponentPeviewComponent {
path = input.required<string>();
styleName = input<string>();
nameExample = input<string>();
examples = examples;

component = computed(async () => {
if (!this.styleName() || !this.nameExample()) return null;

protected component = computed(async () => {
return (await import(/* @vite-ignore */ this.path())).default;
return await examples[this.styleName()!][this.nameExample()!].component();
});
}

0 comments on commit daa2073

Please sign in to comment.