Skip to content

Commit

Permalink
feat: add component preview and source
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-ub committed Jul 27, 2024
1 parent c1615b8 commit db63501
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 16 deletions.
8 changes: 6 additions & 2 deletions apps/www/src/components/ComponentPreview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const components = await Promise.all(
"example",
`${name}.ts`
);
const Component = (await import(/* @vite-ignore */ `${filename}`)).default;
const Component = (
await import(`../registry/${style.name}/example/${name}.ts`)
).default;
const code = readFileSync(`${filename}`)
.toString()
.replace(/export\s+default\s+.*;\s*/, "")
Expand All @@ -44,10 +46,11 @@ const components = await Promise.all(
<div class="flex items-center justify-between p-4">
<select
class="flex items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>option]:line-clamp-1 h-7 w-[145px] text-xs [&_svg]:h-4 [&_svg]:w-4"
id="change-style"
data-target={$("change-theme")}
>
{
styles.map(({ name, label }, index) => (
styles.map(({ name, label }) => (
<option value={name}>{label}</option>
))
}
Expand Down Expand Up @@ -91,6 +94,7 @@ const components = await Promise.all(

$.ready(async () => {
$("change-theme").addEventListener("change", (event) => {
// @ts-expect-error
const value = event.target.value;
hiddeComponents();
$(`preview-${value}`).hidden = false;
Expand Down
69 changes: 69 additions & 0 deletions apps/www/src/components/ComponentSource.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
import path from "path";
import { styles } from "@/registry/styles";
import { ui } from "@/registry/ui";
import { readFileSync } from "fs";
import { Code } from "@astrojs/starlight/components";
export interface Props {
name: string;
}
const { name } = Astro.props;
const componentUi = ui.find((value) => value.name === name);
const allComponents = componentUi?.files.flatMap((file) => {
return styles.map((style) => {
const filename = path.join(
process.cwd(),
"src",
"registry",
style.name,
file
);
const code = readFileSync(`${filename}`)
.toString()
.replace(/export\s+default\s+.*;\s*/, "")
.replace(/@\/registry\/.*\/ui/g, "@/components/ui");
return {
style,
code,
};
});
});
---

{
allComponents?.map(({ code, style }, index) => (
<div
data-target={$("source-" + style.name)}
class="w-[38rem]"
hidden={index !== 0}
>
<Code code={code} class="w-[38rem]" wrap={true} />
</div>
))
}
<script>
import { styles } from "@/registry/styles";

$.ready(async () => {
document
.getElementById("change-style")
?.addEventListener("change", (event) => {
// @ts-expect-error
const value = event.target.value;
hiddeComponents();
$(`source-${value}`).hidden = false;
});

function hiddeComponents() {
styles.forEach(({ name }) => {
$(`source-${name}`).hidden = true;
});
}
});
</script>
14 changes: 13 additions & 1 deletion apps/www/src/content/docs/docs/components/alert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ description: Displays a callout for user attention.
import { Tabs, TabItem, Code } from "@astrojs/starlight/components";
import { Steps } from "@astrojs/starlight/components";
import ComponentPreview from "@/components/ComponentPreview.astro";
import ComponentSource from "@/components/ComponentSource.astro";

<ComponentPreview name="alert-demo" />

## Installation

<Tabs>
<TabItem label="CLI">
```bash
```bash frame="code"
npx shadcn-ng@latest add alert
```
</TabItem>

<TabItem label="Manual">
<Steps>
1. Copy and paste the following code into your project.
<ComponentSource name="alert" />

2. Update the import paths to match your project setup.
</Steps>
Expand All @@ -46,3 +48,13 @@ import {
</div>
</div>
```

## Examples

### Default

<ComponentPreview name="alert-demo" />

### Destructive

<ComponentPreview name="alert-destructive" />
32 changes: 32 additions & 0 deletions apps/www/src/registry/default/example/alert-destructive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Component } from "@angular/core";
import { NgIconComponent, provideIcons } from "@ng-icons/core";
import { lucideTerminal } from "@ng-icons/lucide";

import {
UbAlertDirective,
UbAlertTitleDirective,
UbAlertDescriptionDirective,
} from "@/registry/default/ui/alert.directive";

@Component({
standalone: true,
imports: [
NgIconComponent,
UbAlertDirective,
UbAlertTitleDirective,
UbAlertDescriptionDirective,
],
viewProviders: [provideIcons({ lucideTerminal })],
template: `
<div ubAlert variant="destructive">
<ng-icon name="lucideTerminal" class="h-4 w-4" />
<h5 ubAlertTitle>Heads up!</h5>
<div ubAlertDescription>
You can add components and dependencies to your app using the cli.
</div>
</div>
`,
})
export class AlertDemoComponent {}

export default AlertDemoComponent;
8 changes: 3 additions & 5 deletions apps/www/src/registry/default/ui/alert.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const alertVariants = cva(
);

type AlertProps = VariantProps<typeof alertVariants>;
export type UbAlertVariant = NonNullable<AlertProps["variant"]>;
type UbAlertVariant = NonNullable<AlertProps["variant"]>;

@Directive({
selector: "div[ubAlert]",
Expand All @@ -40,7 +40,6 @@ export class UbAlertDirective {
);
}

const alertTitleVariants = cva("mb-1 font-medium leading-none tracking-tight");
@Directive({
selector: "h5[ubAlertTitle]",
standalone: true,
Expand All @@ -52,11 +51,10 @@ export class UbAlertTitleDirective {
readonly class = input<string>();

protected computedClass = computed(() =>
cn(alertTitleVariants({ class: this.class() }))
cn("mb-1 font-medium leading-none tracking-tight", this.class())
);
}

const alertDescriptionVariants = cva("text-sm [&_p]:leading-relaxed");
@Directive({
selector: "div[ubAlertDescription]",
standalone: true,
Expand All @@ -68,6 +66,6 @@ export class UbAlertDescriptionDirective {
readonly class = input<string>();

protected computedClass = computed(() =>
cn(alertDescriptionVariants({ class: this.class() }))
cn("text-sm [&_p]:leading-relaxed", this.class())
);
}
6 changes: 3 additions & 3 deletions apps/www/src/registry/new-york/example/alert-demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from "@angular/core";
import { NgIconComponent, provideIcons } from "@ng-icons/core";
import { lucideTerminal } from "@ng-icons/lucide";
import { lucideRocket } from "@ng-icons/lucide";

import {
UbAlertDirective,
Expand All @@ -16,10 +16,10 @@ import {
UbAlertTitleDirective,
UbAlertDescriptionDirective,
],
viewProviders: [provideIcons({ lucideTerminal })],
viewProviders: [provideIcons({ lucideRocket })],
template: `
<div ubAlert>
<ng-icon name="lucideTerminal" class="h-4 w-4" />
<ng-icon name="lucideRocket" class="h-4 w-4" />
<h5 ubAlertTitle>Heads up!</h5>
<div ubAlertDescription>
You can add components and dependencies to your app using the cli.
Expand Down
32 changes: 32 additions & 0 deletions apps/www/src/registry/new-york/example/alert-destructive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Component } from "@angular/core";
import { NgIconComponent, provideIcons } from "@ng-icons/core";
import { lucideTerminal } from "@ng-icons/lucide";

import {
UbAlertDirective,
UbAlertTitleDirective,
UbAlertDescriptionDirective,
} from "@/registry/new-york/ui/alert.directive";

@Component({
standalone: true,
imports: [
NgIconComponent,
UbAlertDirective,
UbAlertTitleDirective,
UbAlertDescriptionDirective,
],
viewProviders: [provideIcons({ lucideTerminal })],
template: `
<div ubAlert variant="destructive">
<ng-icon name="lucideTerminal" class="h-4 w-4" />
<h5 ubAlertTitle>Heads up!</h5>
<div ubAlertDescription>
You can add components and dependencies to your app using the cli.
</div>
</div>
`,
})
export class AlertDemoComponent {}

export default AlertDemoComponent;
8 changes: 3 additions & 5 deletions apps/www/src/registry/new-york/ui/alert.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const alertVariants = cva(
);

type AlertProps = VariantProps<typeof alertVariants>;
export type UbAlertVariant = NonNullable<AlertProps["variant"]>;
type UbAlertVariant = NonNullable<AlertProps["variant"]>;

@Directive({
selector: "div[ubAlert]",
Expand All @@ -40,7 +40,6 @@ export class UbAlertDirective {
);
}

const alertTitleVariants = cva("mb-1 font-medium leading-none tracking-tight");
@Directive({
selector: "h5[ubAlertTitle]",
standalone: true,
Expand All @@ -52,11 +51,10 @@ export class UbAlertTitleDirective {
readonly class = input<string>();

protected computedClass = computed(() =>
cn(alertTitleVariants({ class: this.class() }))
cn("mb-1 font-medium leading-none tracking-tight", this.class())
);
}

const alertDescriptionVariants = cva("text-sm [&_p]:leading-relaxed");
@Directive({
selector: "div[ubAlertDescription]",
standalone: true,
Expand All @@ -68,6 +66,6 @@ export class UbAlertDescriptionDirective {
readonly class = input<string>();

protected computedClass = computed(() =>
cn(alertDescriptionVariants({ class: this.class() }))
cn("text-sm [&_p]:leading-relaxed", this.class())
);
}

0 comments on commit db63501

Please sign in to comment.