-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): add autoresize for textarea
- Loading branch information
1 parent
537e29b
commit 9fa6a8b
Showing
17 changed files
with
96 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
import { autoresizeTextarea } from '@zag-js/auto-resize' | ||
import { mergeProps } from '@zag-js/react' | ||
import { forwardRef } from 'react' | ||
import { forwardRef, useEffect, useRef } from 'react' | ||
import { composeRefs } from '../../utils/compose-refs' | ||
import { type HTMLProps, type PolymorphicProps, ark } from '../factory' | ||
import { useFieldContext } from './use-field-context' | ||
|
||
export interface FieldTextareaBaseProps extends PolymorphicProps {} | ||
export interface FieldTextareaBaseProps extends PolymorphicProps { | ||
/** | ||
* Whether the textarea should autoresize | ||
* @default false | ||
*/ | ||
autoresize?: boolean | ||
} | ||
export interface FieldTextareaProps extends HTMLProps<'textarea'>, FieldTextareaBaseProps {} | ||
|
||
export const FieldTextarea = forwardRef<HTMLTextAreaElement, FieldTextareaProps>((props, ref) => { | ||
const { autoresize, ...textareaProps } = props | ||
const textareaRef = useRef<HTMLTextAreaElement>(null) | ||
const field = useFieldContext() | ||
const mergedProps = mergeProps<HTMLProps<'textarea'>>(field?.getTextareaProps(), props) | ||
const mergedProps = mergeProps<HTMLProps<'textarea'>>( | ||
field?.getTextareaProps(), | ||
{ style: { resize: autoresize ? 'none' : undefined } }, | ||
textareaProps, | ||
) | ||
|
||
return <ark.textarea {...mergedProps} ref={ref} /> | ||
useEffect(() => { | ||
if (!autoresize) return | ||
return autoresizeTextarea(textareaRef.current) | ||
}, [autoresize]) | ||
|
||
return <ark.textarea {...mergedProps} ref={composeRefs(ref, textareaRef)} /> | ||
}) | ||
|
||
FieldTextarea.displayName = 'FieldTextarea' |
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 |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import { autoresizeTextarea } from '@zag-js/auto-resize' | ||
import { mergeProps } from '@zag-js/solid' | ||
import { createEffect } from 'solid-js' | ||
import { type HTMLProps, type PolymorphicProps, ark } from '../factory' | ||
import { useFieldContext } from './use-field-context' | ||
|
||
export interface FieldTextareaBaseProps extends PolymorphicProps<'textarea'> {} | ||
export interface FieldTextareaBaseProps extends PolymorphicProps<'textarea'> { | ||
/** | ||
* Whether the textarea should autoresize | ||
* @default false | ||
*/ | ||
autoresize?: boolean | ||
} | ||
export interface FieldTextareaProps extends HTMLProps<'textarea'>, FieldTextareaBaseProps {} | ||
|
||
export const FieldTextarea = (props: FieldTextareaProps) => { | ||
const field = useFieldContext() | ||
const mergedProps = mergeProps(() => field?.().getTextareaProps(), props) | ||
let textareaRef: HTMLTextAreaElement | ||
const { autoresize, ...textareaProps } = props | ||
|
||
const mergedProps = mergeProps( | ||
() => field?.().getTextareaProps(), | ||
() => ({ style: { resize: autoresize ? 'none' : undefined } }), | ||
textareaProps, | ||
) | ||
|
||
createEffect(() => { | ||
if (!autoresize) return | ||
const cleanup = autoresizeTextarea(textareaRef) | ||
return cleanup | ||
}) | ||
|
||
return <ark.textarea {...mergedProps} /> | ||
return ( | ||
<ark.textarea | ||
{...mergedProps} | ||
ref={(el) => { | ||
textareaRef = el | ||
}} | ||
/> | ||
) | ||
} |
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
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