Skip to content

Commit

Permalink
feat: Added new Typewriter component
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-crowell committed Jun 18, 2024
1 parent 067a5cc commit b76037a
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/ui/src/components/Typewriter/Typewriter.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Meta, StoryObj } from '@storybook/react';

import { Typewriter } from './Typewriter';

const meta = {
component: Typewriter,
} satisfies Meta<typeof Typewriter>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
words: [ 'Hello, World!', 'This is the do-ob Typewriter component.' ]
}
};
51 changes: 51 additions & 0 deletions packages/ui/src/components/Typewriter/Typewriter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useTypewriter } from '@do-ob/ui/hooks';

export interface TypewriterProps extends React.HTMLAttributes<HTMLParagraphElement> {
/**
* The words to type
*/
words: string | string[];

/**
* Number of times to loop through the words.
*/
loop?: number;

/**
* The speed of the typing
*/
typeSpeed?: number;

/**
* The speed of the backspacing
*/
deleteSpeed?: number;

/**
* The deltay between typing the next word.
*/
wordDelay?: number;
}


export function Typewriter({
words: wordsProp,
loop = 0,
typeSpeed = 80,
deleteSpeed = 50,
wordDelay = 1500,
...props
}: TypewriterProps) {

const words = Array.isArray(wordsProp) ? wordsProp : [ wordsProp ];

const [ text ] = useTypewriter({
words,
loop,
typeSpeed,
deleteSpeed,
wordDelay
});

return <p {...props}>{text}</p>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Meta, StoryObj } from '@storybook/react';

import { TypewriterInput } from './TypewriterInput';

const meta = {
component: TypewriterInput,
} satisfies Meta<typeof TypewriterInput>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
words: [ 'Hello, World!', 'This is the do-ob Typewriter Input component.' ]
}
};
57 changes: 57 additions & 0 deletions packages/ui/src/components/TypewriterInput/TypewriterInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useTypewriter } from '@do-ob/ui/hooks';
import { Input, InputProps } from '@nextui-org/react';

export interface TypewriterInputProps extends InputProps {
/**
* The words to type
*/
words: string | string[];

/**
* Number of times to loop through the words.
*/
loop?: number;

/**
* The speed of the typing
*/
typeSpeed?: number;

/**
* The speed of the backspacing
*/
deleteSpeed?: number;

/**
* The deltay between typing the next word.
*/
wordDelay?: number;
}


export function TypewriterInput({
words: wordsProp,
loop = 0,
typeSpeed = 80,
deleteSpeed = 50,
wordDelay = 1500,
...props
}: TypewriterInputProps) {

const words = Array.isArray(wordsProp) ? wordsProp : [ wordsProp ];

const [ text ] = useTypewriter({
words,
loop,
typeSpeed,
deleteSpeed,
wordDelay
});

return (
<Input
{...props}
placeholder={text}
/>
);
}

0 comments on commit b76037a

Please sign in to comment.