-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
feat(textarea): auto resizable #1681
Open
rainyEra
wants to merge
16
commits into
shadcn-ui:main
Choose a base branch
from
rainyEra:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−102
Open
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
926b01b
feat: resizable textarea
rainyEra fe45978
chore: new-york textarea update
rainyEra 7c8fa95
refactor: new yorker text-area to initial
rainyEra 0412969
Merge branch 'main' into main
shadcn 7611b81
Merge branch 'main' into main
shadcn 221fb82
fix(textarea): formatting
rainyEra e6d80a3
fix(textarea): textarea: use client useRef
rainyEra 065e3a2
(textarea)fix: use client w useRef
rainyEra ce1f145
(textarea)fix: use client w useRef
rainyEra e97b9ed
Merge branch 'main' of https://github.com/rainyEra/ui
rainyEra 717aca7
refactor: resize hook, -1 dependency
rainyEra e99e689
feat(ui): text-area autoResize prop
rainyEra ce98b49
refactor(ui): textArea format
rainyEra 6f90791
Merge branch 'shadcn-ui:main' into main
rainyEra c7f26c5
docs(ui): textarea autoResize prop
rainyEra 1485410
build(ui): textarea-with-autoresize.tsx
rainyEra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
'use client' | ||
import * as React from "react" | ||
import { useEffect, useRef } from "react" | ||
import { mergeRefs } from "react-merge-refs" | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
export interface TextareaProps | ||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {} | ||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { } | ||
|
||
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>( | ||
({ className, ...props }, ref) => { | ||
const texteAreaRef = useRef<HTMLTextAreaElement>(null) | ||
|
||
useEffect(() => { | ||
const ref = texteAreaRef?.current | ||
|
||
const updateTextareaHeight = () => { | ||
if (ref) { | ||
ref.style.height = "auto" | ||
ref.style.height = ref?.scrollHeight + "px" | ||
} | ||
} | ||
|
||
updateTextareaHeight() | ||
ref?.addEventListener("input", updateTextareaHeight) | ||
|
||
return () => ref?.removeEventListener("input", updateTextareaHeight) | ||
}, []) | ||
|
||
return ( | ||
<textarea | ||
className={cn( | ||
"flex min-h-[80px] w-full 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 | ||
)} | ||
ref={ref} | ||
ref={mergeRefs([texteAreaRef, ref])} | ||
{...props} | ||
/> | ||
) | ||
} | ||
) | ||
Textarea.displayName = "Textarea" | ||
|
||
export { Textarea } | ||
Textarea.displayName = "Textarea" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use
React.useImperativeHandle
to merge the refs so you don't need to pull in an extra dependency.https://react.dev/reference/react/useImperativeHandle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it, I just prefer other way due to readability. -1 dependency, this is also good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how can i make a dynamic textarea with react-hook-form, it handles ref by itself . how can i make it use my ref