Skip to content

Commit

Permalink
Add test story
Browse files Browse the repository at this point in the history
  • Loading branch information
lancesnider committed Dec 5, 2024
1 parent 1f6ae83 commit 5e80ebf
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
"stories": [
"../examples/stories/*.stories.mdx",
"../examples/stories/*.stories.@(js|jsx|ts|tsx)"
"../examples/**/*.stories.mdx",
"../examples/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
Expand Down
Binary file added examples/stories/assets/map-accessories.riv
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/stories/components/bareBonesRiveCanvas/Example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
canvas {
width: 500px!important;
height: 500px!important;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Meta, StoryObj } from '@storybook/react';

import BareBonesRiveCanvas from './Example';

const meta = {
title: 'Components/BareBonesRiveCanvas',
component: BareBonesRiveCanvas,
argTypes: {
backgroundColor: { control: 'color' },
},
} satisfies Meta<typeof BareBonesRiveCanvas>;

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

export const Primary: Story = {
args: {},
};
102 changes: 102 additions & 0 deletions examples/stories/components/bareBonesRiveCanvas/Example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React, { useState, useRef, useEffect } from 'react';

import Bird from './components/Bird';
import Tree from './components/Tree';

export default function App() {
return (
<div className="flex size-full justify-center items-center gap-4">
<RiveTest />
<StaticTest />
</div>
);
}

function StaticTest() {
const [counter, setCounter] = useState<number>(0);

const [running, setRunning] = useState<boolean>(false);
const intervalRef = useRef<number>();
useEffect(() => {
if (running) {
intervalRef.current = setInterval(() => {
setCounter((prev) => prev + 1);
}, 100);
} else {
clearInterval(intervalRef.current);
}
}, [running]);

return (
<div className="size-full flex items-center justify-center flex-col gap-4">
<h1>Static</h1>
<Button onClick={() => setRunning((prev) => !prev)}>
{running ? 'remounting...' : 'Start'}
</Button>
<FakeAsset id="🌲" key={counter + 'tree'} />
<FakeAsset id="πŸ“" key={counter + 'bird'} />
</div>
);
}

function RiveTest() {
const [counter, setCounter] = useState<number>(0);

const [running, setRunning] = useState<boolean>(false);
const intervalRef = useRef<number>();
useEffect(() => {
if (running) {
intervalRef.current = setInterval(() => {
setCounter((prev) => prev + 1);
}, 100);
} else {
clearInterval(intervalRef.current);
}
}, [running]);

return (
<div className="size-full flex items-center justify-center flex-col gap-4">
<h1>Rive</h1>
<Button onClick={() => setRunning((prev) => !prev)}>
{running ? 'remounting...' : 'Start'}
</Button>
<Tree key={counter + 'tree'} />
<Bird key={counter + 'bird'} />
</div>
);
}

const useId = () => {
// random id
return Math.random().toString(36).substring(7);
};

function FakeAsset({ id }: { id: string; key: string }) {
const internalId = useId();
useEffect(() => {
console.log(`[${internalId}] mount static ${id}`);
return () => {
console.log(`[${internalId}] unmount static ${id}`);
};
}, [id, internalId]);

return (
<div
id={`Static-${id}`}
className="size-[100px] ring-1 text-2xl flex items-center justify-center"
>
{id}
</div>
);
}

// ---
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement>;

function Button({ children, ...props }: ButtonProps) {
return (
<button className="ring-1 p-4" {...props}>
{children}
</button>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useRive } from '../../../../../src';
import React, { useEffect } from 'react';

export default function Bird({}) {
const { RiveComponent } = useRive({
stateMachines: 'State Machine 1',
artboard: 'bird',
src: 'map-accessories.riv',
autoplay: true,
});

useEffect(() => {
// console.log(`mount rive πŸ“`);
return () => {
// console.log(`unmount rive πŸ“`);
};
}, []);

return (
<div id="Rive-πŸ“" className="size-[100px] ring-1">
<RiveComponent />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useRive } from '../../../../../src';

import React, { useEffect } from 'react';

export default function Tree({}) {
const { RiveComponent } = useRive({
stateMachines: 'State Machine 1',
artboard: 'treeComponent',
src: 'map-accessories.riv',
autoplay: true,
});

useEffect(() => {
// console.log(`mount rive 🌲`);
return () => {
// console.log(`unmount rive 🌲`);
};
}, []);

return (
<div id="Rive-🌲" className="size-[100px] ring-1">
<RiveComponent />
</div>
);
}
Binary file not shown.

0 comments on commit 5e80ebf

Please sign in to comment.