Skip to content

Commit

Permalink
feat: adds auto resize feature to textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
LimChorngUan committed Jan 27, 2025
1 parent 344b231 commit ddc525d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
24 changes: 13 additions & 11 deletions packages/ui/src/fields/Textarea/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ export const TextareaInput: React.FC<TextAreaInputProps> = (props) => {
<label className="textarea-outer" htmlFor={`field-${path.replace(/\./g, '__')}`}>
<div className="textarea-inner">
<div className="textarea-clone" data-value={value || placeholder || ''} />
<textarea
className="textarea-element"
data-rtl={rtl}
disabled={readOnly}
id={`field-${path.replace(/\./g, '__')}`}
name={path}
onChange={onChange}
placeholder={getTranslation(placeholder, i18n)}
rows={rows}
value={value || ''}
/>
<div className="textarea-grow-wrap" data-value={value || placeholder || ''}>
<textarea
className="textarea-element"
data-rtl={rtl}
disabled={readOnly}
id={`field-${path.replace(/\./g, '__')}`}
name={path}
onChange={onChange}
placeholder={getTranslation(placeholder, i18n)}
rows={rows}
value={value || ''}
/>
</div>
</div>
</label>
{AfterInput}
Expand Down
22 changes: 22 additions & 0 deletions packages/ui/src/fields/Textarea/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@
@include mid-break {
padding: 0;
}

// For auto resize
// Once the textarea is resized manually by the browser default drag handler, the auto resize is not working anymore
.textarea-grow-wrap {
display: grid;

&::after {
content: attr(data-value) ' ';
white-space: pre-wrap;
visibility: hidden;
}

& > textarea {
overflow: hidden;
}

& > textarea,
&::after {
font: inherit;
grid-area: 1 / 1 / 2 / 2;
}
}
}

html[data-theme='light'] {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/fields/Textarea/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type ChangeEvent } from 'react'

export type TextAreaInputProps = {
readonly AfterInput?: React.ReactNode
readonly autoResize?: boolean
readonly BeforeInput?: React.ReactNode
readonly className?: string
readonly Description?: React.ReactNode
Expand Down
4 changes: 4 additions & 0 deletions test/_community/collections/Posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const PostsCollection: CollectionConfig = {
name: 'title',
type: 'text',
},
{
name: 'comment',
type: 'textarea',
},
{
name: 'content',
type: 'richText',
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
],
"paths": {
"@payload-config": ["./test/admin/config.ts"],
"@payload-config": ["./test/_community/config.ts"],
"@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],
Expand Down

0 comments on commit ddc525d

Please sign in to comment.