Skip to content
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

📚 /tmp2 に Canvas をおいてたのを Storybook に移した #23

Merged
merged 1 commit into from
Jun 14, 2024
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
129 changes: 129 additions & 0 deletions apps/client/app/features/artboard/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import type { Meta, StoryObj } from "@storybook/react";
import { useRef } from "react";
import { Artboard } from ".";
import type { PenType } from "./types";
import { useArtboard } from "./useArtboard";

const meta: Meta<typeof ArtboardWithHooks> = {
title: "features/Artboard",
component: ArtboardWithHooks,
tags: ["autodocs"],
parameters: {
docs: {
description: {
component: `\`useArtboard\` と \`Artboard\` を使ったお絵かきキャンバス

通常 \`useRef\` で作成した \`canvasRef\` とペンの設定を \`useArtboard\` に渡し、\`canvasRef\` と \`useArtboard\` で得られた \`mouseHandler\` を \`Artboard\` に渡して使う。

\`useArtboard\` 内の他の返り値を用いて、キャンバスの reset, clear, undo, redo などを実装することができる。`,
},
},
},
};
export default meta;

type Story = StoryObj<typeof ArtboardWithHooks>;

export const Primary: Story = {
args: {
penType: "pen",
penSize: 10,
penColor: "#000000",
isLocked: false,
},
argTypes: {
penType: {
options: [
"pen",
"eraser",
"bucket",
] as const satisfies readonly PenType[],
control: {
type: "radio",
},
},
penSize: {
control: {
type: "range",
min: 1,
max: 100,
step: 10,
},
},
penColor: {
control: {
type: "color",
// FIXME: 値はとりあえず適当
presetColors: [
"#000000",
"#ff0000",
"#00ff00",
"#0000ff",
"#ffff00",
"#ff00ff",
"#00ffff",
],
},
},
isLocked: {
control: {
type: "boolean",
},
},
},
render: (args) => {
return <ArtboardWithHooks {...args} />;
},
};

function ArtboardWithHooks({
penType,
penSize,
penColor,
isLocked,
}: {
penType: PenType;
penSize: number;
penColor: `#${string}`;
isLocked: boolean;
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const {
canRedo,
canUndo,
clear,
redo,
resetCanvas,
shortcut,
undo,
mouseHandlers,
} = useArtboard(canvasRef, {
color: penColor,
size: penSize,
type: penType,
});

return (
<div onKeyDown={shortcut}>
<div>
<button type="button" onClick={resetCanvas}>
Reset
</button>
<button type="button" onClick={clear}>
Clear
</button>
<button type="button" onClick={undo} disabled={!canUndo}>
Undo
</button>
<button type="button" onClick={redo} disabled={!canRedo}>
Redo
</button>
</div>
<Artboard
canvasRef={canvasRef}
mouseHandlers={mouseHandlers}
isLocked={isLocked}
/>
</div>
);
}
88 changes: 0 additions & 88 deletions apps/client/app/routes/tmp2.tsx

This file was deleted.