-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into feature/midaz-logg…
…er-v1
- Loading branch information
Showing
12 changed files
with
307 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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
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,19 @@ | ||
import { Meta, Controls, Primary, Canvas } from '@storybook/blocks' | ||
import * as Story from './autosize-textarea.stories' | ||
|
||
<Meta of={Story} /> | ||
|
||
# Auto Size Textarea | ||
|
||
Auto resize textarea height based on content. \ | ||
[Docs](https://shadcnui-expansions.typeart.cc/docs/autosize-textarea) | ||
|
||
### Default | ||
|
||
<Canvas of={Story.Default} /> | ||
|
||
### With Max Height | ||
|
||
<Canvas of={Story.MaxHeight} /> | ||
|
||
<Controls /> |
50 changes: 50 additions & 0 deletions
50
src/components/ui/autosize-textarea/autosize-textarea.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,50 @@ | ||
import { ComponentProps } from 'react' | ||
import { Meta, StoryObj } from '@storybook/react' | ||
import { AutosizeTextarea, AutosizeTextAreaProps } from '.' | ||
import { FormProvider, useForm } from 'react-hook-form' | ||
|
||
const meta: Meta<AutosizeTextAreaProps> = { | ||
title: 'Primitives/AutosizeTextarea', | ||
component: AutosizeTextarea, | ||
argTypes: { | ||
disabled: { | ||
type: 'boolean', | ||
description: 'If the input is disabled' | ||
}, | ||
className: { | ||
type: 'string', | ||
description: "The input's class" | ||
} | ||
} | ||
} | ||
|
||
export default meta | ||
|
||
export const Default: StoryObj<AutosizeTextAreaProps> = { | ||
args: { | ||
placeholder: 'This textarea with min height 52 and unlimited max height.' | ||
}, | ||
render: (args) => { | ||
const form = useForm() | ||
return ( | ||
<FormProvider {...form}> | ||
<AutosizeTextarea {...args} /> | ||
</FormProvider> | ||
) | ||
} | ||
} | ||
|
||
export const MaxHeight: StoryObj<AutosizeTextAreaProps> = { | ||
args: { | ||
placeholder: 'This textarea with min height 52 and max height 200.', | ||
maxHeight: 200 | ||
}, | ||
render: (args) => { | ||
const form = useForm() | ||
return ( | ||
<FormProvider {...form}> | ||
<AutosizeTextarea {...args} /> | ||
</FormProvider> | ||
) | ||
} | ||
} |
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,112 @@ | ||
'use client' | ||
|
||
import * as React from 'react' | ||
import { cn } from '@/lib/utils' | ||
import { useImperativeHandle } from 'react' | ||
|
||
interface UseAutosizeTextAreaProps { | ||
textAreaRef: React.MutableRefObject<HTMLTextAreaElement | null> | ||
minHeight?: number | ||
maxHeight?: number | ||
triggerAutoSize: string | ||
} | ||
|
||
export const useAutosizeTextArea = ({ | ||
textAreaRef, | ||
triggerAutoSize, | ||
maxHeight = Number.MAX_SAFE_INTEGER, | ||
minHeight = 0 | ||
}: UseAutosizeTextAreaProps) => { | ||
const [init, setInit] = React.useState(true) | ||
React.useEffect(() => { | ||
// We need to reset the height momentarily to get the correct scrollHeight for the textarea | ||
const textAreaElement = textAreaRef.current | ||
if (textAreaElement) { | ||
if (init) { | ||
textAreaElement.style.minHeight = `${minHeight}px` | ||
if (maxHeight > minHeight) { | ||
textAreaElement.style.maxHeight = `${maxHeight}px` | ||
} | ||
setInit(false) | ||
} | ||
textAreaElement.style.height = `${minHeight}px` | ||
const scrollHeight = textAreaElement.scrollHeight | ||
|
||
// We then set the height directly, outside of the render loop | ||
// Trying to set this with state or a ref will product an incorrect value. | ||
if (scrollHeight > maxHeight) { | ||
textAreaElement.style.height = `${maxHeight}px` | ||
textAreaElement.style.overflowY = 'auto' | ||
} else { | ||
textAreaElement.style.height = `${scrollHeight}px` | ||
textAreaElement.style.overflowY = 'hidden' | ||
} | ||
} | ||
}, [textAreaRef.current, triggerAutoSize]) | ||
} | ||
|
||
export type AutosizeTextAreaRef = { | ||
textArea: HTMLTextAreaElement | ||
maxHeight: number | ||
minHeight: number | ||
} | ||
|
||
export type AutosizeTextAreaProps = { | ||
maxHeight?: number | ||
minHeight?: number | ||
} & React.TextareaHTMLAttributes<HTMLTextAreaElement> | ||
|
||
export const AutosizeTextarea = React.forwardRef< | ||
AutosizeTextAreaRef, | ||
AutosizeTextAreaProps | ||
>( | ||
( | ||
{ | ||
maxHeight = Number.MAX_SAFE_INTEGER, | ||
minHeight = 36, | ||
className, | ||
onChange, | ||
value, | ||
...props | ||
}: AutosizeTextAreaProps, | ||
ref: React.Ref<AutosizeTextAreaRef> | ||
) => { | ||
const textAreaRef = React.useRef<HTMLTextAreaElement | null>(null) | ||
const [triggerAutoSize, setTriggerAutoSize] = React.useState('') | ||
|
||
useAutosizeTextArea({ | ||
textAreaRef, | ||
triggerAutoSize: triggerAutoSize, | ||
maxHeight, | ||
minHeight | ||
}) | ||
|
||
useImperativeHandle(ref, () => ({ | ||
textArea: textAreaRef.current as HTMLTextAreaElement, | ||
focus: () => textAreaRef?.current?.focus(), | ||
maxHeight, | ||
minHeight | ||
})) | ||
|
||
React.useEffect(() => { | ||
setTriggerAutoSize(value as string) | ||
}, [props?.defaultValue, value]) | ||
|
||
return ( | ||
<textarea | ||
{...props} | ||
value={value} | ||
ref={textAreaRef} | ||
className={cn( | ||
'flex h-9 w-full overflow-y-hidden rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50', | ||
className | ||
)} | ||
onChange={(e) => { | ||
setTriggerAutoSize(e.target.value) | ||
onChange?.(e) | ||
}} | ||
/> | ||
) | ||
} | ||
) | ||
AutosizeTextarea.displayName = 'AutosizeTextarea' |
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
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
Oops, something went wrong.