-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added new Typewriter component
- Loading branch information
1 parent
067a5cc
commit b76037a
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/ui/src/components/Typewriter/Typewriter.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' ] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/ui/src/components/TypewriterInput/TypewriterInput.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
packages/ui/src/components/TypewriterInput/TypewriterInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
); | ||
} |