-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref: growing input is no more #86553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
16e0928
move growing input
JonasBa 91ccedf
growinginput: move growing functionality to a hook
JonasBa e34b776
growinginput: use input html attributes
JonasBa b2dc249
input: change to autosize prop
JonasBa 2ecedf1
provide default onchagne handler in input
JonasBa 4d48c22
input requires onchange
JonasBa c54b7af
Merge branch 'master' into jb/components/growing-input
JonasBa 9b42536
input: separate interface and optional chain removeeventlistener
JonasBa 4e14fec
Mpickerge branch 'master' into jb/components/growing-input
JonasBa 0f1dfca
input: remove autosize prop from input
JonasBa c357bd2
input: fix story
JonasBa 61eff3a
input: remove last occurrence of growingInput
JonasBa cd81c74
Merge branch 'master' into jb/components/growing-input
JonasBa b4a9f00
input: attach autosize to input
JonasBa 8e4118e
input: remove hook interface export
JonasBa 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 hidden or 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 hidden or 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,94 @@ | ||
| import type React from 'react'; | ||
| import {useCallback, useLayoutEffect, useRef} from 'react'; | ||
|
|
||
| /** | ||
| * This hook is used to automatically resize an input element based on its content. | ||
| * It is useful for creating "growing" inputs that can resize to fit their content. | ||
| * | ||
| * @param options - Options for the autosize input functionality. | ||
| * @param options.disabled - Set to `true` to disable the autosizing. | ||
| * @param options.value - The value of the input, use when the input is controlled. | ||
| * @returns A ref callback for the input element. | ||
| */ | ||
|
|
||
| interface UseAutosizeInputOptions { | ||
| enabled?: boolean; | ||
| value?: React.InputHTMLAttributes<HTMLInputElement>['value'] | undefined; | ||
| } | ||
|
|
||
| export function useAutosizeInput( | ||
| options?: UseAutosizeInputOptions | ||
| ): React.RefCallback<HTMLInputElement> { | ||
| const enabled = options?.enabled ?? true; | ||
| const sourceRef = useRef<HTMLInputElement | null>(null); | ||
|
|
||
| // A controlled input value change does not trigger a change event, | ||
| // so we need to manually observe the value... | ||
| useLayoutEffect(() => { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
|
|
||
| if (sourceRef.current) { | ||
| resize(sourceRef.current); | ||
| } | ||
| }, [options?.value, enabled]); | ||
|
|
||
| const onInputChange = useCallback((_event: any) => { | ||
| if (sourceRef.current) { | ||
| resize(sourceRef.current); | ||
| } | ||
| }, []); | ||
|
|
||
| const autosizingCallbackRef: React.RefCallback<HTMLInputElement> = useCallback( | ||
| (element: HTMLInputElement | null) => { | ||
| if (!enabled || !element) { | ||
| sourceRef.current?.removeEventListener('input', onInputChange); | ||
| } else { | ||
| resize(element); | ||
| element.addEventListener('input', onInputChange); | ||
| } | ||
|
|
||
| sourceRef.current = element; | ||
| }, | ||
| [onInputChange, enabled] | ||
| ); | ||
|
|
||
| return autosizingCallbackRef; | ||
| } | ||
|
|
||
| function createSizingDiv(referenceStyles: CSSStyleDeclaration) { | ||
| const sizingDiv = document.createElement('div'); | ||
| sizingDiv.style.whiteSpace = 'pre'; | ||
| sizingDiv.style.width = 'auto'; | ||
| sizingDiv.style.height = '0'; | ||
| sizingDiv.style.position = 'fixed'; | ||
| sizingDiv.style.pointerEvents = 'none'; | ||
| sizingDiv.style.opacity = '0'; | ||
| sizingDiv.style.zIndex = '-1'; | ||
|
|
||
| sizingDiv.style.fontSize = referenceStyles.fontSize; | ||
| sizingDiv.style.fontWeight = referenceStyles.fontWeight; | ||
| sizingDiv.style.fontFamily = referenceStyles.fontFamily; | ||
|
|
||
| return sizingDiv; | ||
| } | ||
|
|
||
| function resize(input: HTMLInputElement) { | ||
| const computedStyles = getComputedStyle(input); | ||
|
|
||
| const sizingDiv = createSizingDiv(computedStyles); | ||
| sizingDiv.innerText = input.value || input.placeholder; | ||
| document.body.appendChild(sizingDiv); | ||
|
|
||
| const newTotalInputSize = | ||
| sizingDiv.offsetWidth + | ||
| // parseInt is save here as the computed styles are always in px | ||
| parseInt(computedStyles.paddingLeft ?? 0, 10) + | ||
| parseInt(computedStyles.paddingRight ?? 0, 10) + | ||
| parseInt(computedStyles.borderWidth ?? 0, 10) * 2 + | ||
| 1; // Add 1px to account for cursor width in Safari | ||
|
|
||
| document.body.removeChild(sizingDiv); | ||
| input.style.width = `${newTotalInputSize}px`; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
I wonder if we can just use
calc(${n}ch + paddinginstead of querying the width. If we can, then we might be able to remove other parts of the sizing functionality as wellThere 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.
Update: we cant because ch unit is apparently based on the the 0 character. When I tested this, I was seeing a drift between the input and width which grew with the size of the input