Skip to content

Commit

Permalink
Update component generators
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonhawk committed Nov 21, 2024
1 parent 3345218 commit 07d454d
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 22 deletions.
63 changes: 56 additions & 7 deletions turbo/generators/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
plop.setGenerator("ui/component", {
description: "A new UI component",
prompts: [
{
type: "confirm",
name: "core",
message: "Is this a core component?",
default: false,
},

{
type: "input",
name: "component_name",
Expand All @@ -22,12 +29,54 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
},
},
],
actions: [
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/{{ dashCase component_name }}.tsx",
templateFile: "templates/ui/component/index.hbs",
},
],

actions: (asdf) => {
const isCoreComponent = asdf?.core ?? false;

if (isCoreComponent) {
return [
// components/core/<component>/index.ts
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/core/{{ dashCase component_name }}/index.ts",
templateFile: "templates/ui/component/core/index.ts.hbs",
},
// components/core/<component>/<component>.tsx
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/core/{{ dashCase component_name }}/{{ dashCase component_name }}.tsx",
templateFile: "templates/ui/component/core/component.tsx.hbs",
},
// components/core/<component>/<component>.stories.tsx
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/core/{{ dashCase component_name }}/{{ dashCase component_name }}.stories.tsx",
templateFile:
"templates/ui/component/core/component.stories.tsx.hbs",
},
];
}

return [
// components/<component>/index.ts
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/{{ dashCase component_name }}/index.ts",
templateFile: "templates/ui/component/index.ts.hbs",
},
// components/<component>/<component>.tsx
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/{{ dashCase component_name }}/{{ dashCase component_name }}.tsx",
templateFile: "templates/ui/component/component.tsx.hbs",
},
// components/<component>/<component>.stories.tsx
{
type: "add",
path: "{{ turbo.paths.root }}/packages/ui/src/components/{{ dashCase component_name }}/{{ dashCase component_name }}.stories.tsx",
templateFile: "templates/ui/component/component.stories.tsx.hbs",
},
];
},
});
}
21 changes: 21 additions & 0 deletions turbo/generators/templates/ui/component/component.stories.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Meta, StoryObj } from "@storybook/react";

import { {{ component_name }} } from "./{{ dashCase component_name }}";

const meta = {
title: "UI/{{ component_name }}",
component: {{ component_name }},
tags: ["autodocs"],
parameters: {
layout: "centered",
},
} satisfies Meta<typeof {{ component_name }}>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
children: null,
},
};
8 changes: 8 additions & 0 deletions turbo/generators/templates/ui/component/component.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type {{ component_name }}Props = {
className?: string;
children: React.ReactNode;
};

export function {{ component_name }}({ className, children }: {{ component_name }}Props) {
return <div className={className}>{children}</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Meta, StoryObj } from "@storybook/react";

import { {{ component_name }} } from "./{{ dashCase component_name }}";

const meta = {
title: "UI/Core/{{ component_name }}",
component: {{ component_name }},
tags: ["autodocs"],
parameters: {
layout: "centered",
},
} satisfies Meta<typeof {{ component_name }}>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
children: null,
},
};
17 changes: 17 additions & 0 deletions turbo/generators/templates/ui/component/core/component.tsx.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { forwardRef } from "react";

import { cn } from "#lib/utils.js";

export interface {{ component_name }}Props extends React.HTMLAttributes<HTMLDivElement> {}

export const {{ component_name }} = forwardRef<HTMLDivElement, {{ component_name }}Props>(
({ className, children, ...props }) => {
return (
<div className={cn(className)} {...props}>
{children}
</div>
);
},
);

{{ component_name }}.displayName = "{{ component_name }}";
1 change: 1 addition & 0 deletions turbo/generators/templates/ui/component/core/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./{{ dashCase component_name }}"
15 changes: 0 additions & 15 deletions turbo/generators/templates/ui/component/index.hbs

This file was deleted.

1 change: 1 addition & 0 deletions turbo/generators/templates/ui/component/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./{{ dashCase component_name }}"

0 comments on commit 07d454d

Please sign in to comment.