Skip to content

Commit

Permalink
feat(*): add autoresize for textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
cschroeter committed Nov 8, 2024
1 parent 537e29b commit 9fa6a8b
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 36 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ description: All notable changes will be documented in this file.

# [Unreleased]

## Added

- **Field**: Added `autoresize` prop to `Field.Textarea` for auto-resizing the textarea based on
content.

## Changed

- **TreeView**: Redesigned using the new tree collection for improved rendering and logic
Expand Down
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"@internationalized/date": "3.5.6",
"@zag-js/accordion": "0.77.0",
"@zag-js/anatomy": "0.77.0",
"@zag-js/auto-resize": "0.77.0",
"@zag-js/avatar": "0.77.0",
"@zag-js/carousel": "0.77.0",
"@zag-js/checkbox": "0.77.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/field/examples/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const Textarea = () => {
return (
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Textarea />
<Field.Textarea autoresize />
<Field.HelperText>Some additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
Expand Down
27 changes: 23 additions & 4 deletions packages/react/src/components/field/field-textarea.tsx
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'
9 changes: 7 additions & 2 deletions packages/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ description: All notable changes will be documented in this file.

## [Unreleased]

## Added

- **Field**: Added `autoresize` prop to `Field.Textarea` for auto-resizing the textarea based on
content.

## Changed

- **TreeView**: Redesigned using the new tree collection for improved rendering and logic
management. See the [TreeView documentation](https://ark-ui.com/docs/solid/components/tree-view) for
details.
management. See the [TreeView documentation](https://ark-ui.com/docs/solid/components/tree-view)
for details.
- **QrCode, SignaturePad, Timer**: Promoted from preview to stable release.

## Fixed
Expand Down
1 change: 1 addition & 0 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"@internationalized/date": "3.5.6",
"@zag-js/accordion": "0.77.0",
"@zag-js/anatomy": "0.77.0",
"@zag-js/auto-resize": "0.77.0",
"@zag-js/avatar": "0.77.0",
"@zag-js/carousel": "0.77.0",
"@zag-js/checkbox": "0.77.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/solid/src/components/field/examples/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const Textarea = () => {
return (
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Textarea />
<Field.Textarea autoresize />
<Field.HelperText>Some additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
Expand Down
34 changes: 31 additions & 3 deletions packages/solid/src/components/field/field-textarea.tsx
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
}}
/>
)
}
5 changes: 5 additions & 0 deletions packages/vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ description: All notable changes will be documented in this file.

## [Unreleased]

## Added

- **Field**: Added `autoresize` prop to `Field.Textarea` for auto-resizing the textarea based on
content.

## Changed

- **TreeView**: Redesigned using the new tree collection for improved rendering and logic
Expand Down
1 change: 1 addition & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
"@internationalized/date": "3.5.6",
"@zag-js/accordion": "0.77.0",
"@zag-js/anatomy": "0.77.0",
"@zag-js/auto-resize": "0.77.0",
"@zag-js/avatar": "0.77.0",
"@zag-js/carousel": "0.77.0",
"@zag-js/checkbox": "0.77.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/components/field/examples/textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Field } from '@ark-ui/vue/field'
<template>
<Field.Root>
<Field.Label>Label</Field.Label>
<Field.Textarea />
<Field.Textarea autoresize />
<Field.HelperText>Some additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
Expand Down
24 changes: 16 additions & 8 deletions packages/vue/src/components/field/field-textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import type { TextareaHTMLAttributes } from 'vue'
import type { PolymorphicProps } from '../factory'
export interface FieldTextareaBaseProps extends PolymorphicProps {}
export interface FieldTextareaBaseProps extends PolymorphicProps {
/**
* Whether the textarea should autoresize
* @default false
*/
autoresize?: boolean
}
export interface FieldTextareaProps
extends FieldTextareaBaseProps,
/**
Expand All @@ -14,24 +20,26 @@ export interface FieldTextareaProps
</script>

<script setup lang="ts">
import { ark } from '../factory'
import { useFieldContext } from './use-field-context'
import { autoresizeTextarea } from '@zag-js/auto-resize'
import { useForwardExpose } from '../../utils'
import { useFieldContext } from './use-field-context'
defineProps<FieldTextareaProps>()
const props = defineProps<FieldTextareaProps>()
const field = useFieldContext()
const emit = defineEmits(['update:modelValue'])
useForwardExpose()
</script>

<!-- TODO use ark.textarea -->
<template>
<ark.textarea
<textarea
:ref="autoresizeTextarea"
v-bind="field.getTextareaProps()"
:value="modelValue"
@input="(event) => emit('update:modelValue', (event.target as HTMLTextAreaElement).value)"
:as-child
:style="props.autoresize ? { resize: 'none', overflow: 'hidden' } : undefined"
>
<slot />
</ark.textarea>
</template>
</textarea>
</template>
8 changes: 1 addition & 7 deletions packages/vue/src/components/select/examples/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ const collection = createListCollection({
</Select.Control>
<Teleport to="body">
<Select.Positioner>
<Select.Content
:ref="
(el) => {
console.log(el)
}
"
>
<Select.Content>
<Select.ItemGroup>
<Select.ItemGroupLabel>Frameworks</Select.ItemGroupLabel>
<Select.Item v-for="item in collection.items" :key="item" :item="item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ const select = useSelect({ collection: collection })
</Select.Control>
<Teleport to="body">
<Select.Positioner>
<Select.Content
:ref="
(el) => {
console.log(el)
}
"
>
<Select.Content>
<Select.ItemGroup>
<Select.ItemGroupLabel>Frameworks</Select.ItemGroupLabel>
<Select.Item v-for="item in collection.items" :key="item" :item="item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { createSplitProps } from '../create-split-props'
const splitVisibilityProps = createSplitProps<VisibilityProps>()
defineProps<TreeViewBranchContentProps>()
const treeView = useTreeViewContext()
const ndoeProps = useTreeViewNodePropsContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface TreeViewRootProviderProps<T extends TreeNode>

<script setup lang="ts" generic="T extends TreeNode">
import { computed } from 'vue'
import { RenderStrategyPropsProvider, useForwardExpose, } from '../../utils'
import { RenderStrategyPropsProvider, useForwardExpose } from '../../utils'
import { ark } from '../factory'
import { TreeViewProvider } from './use-tree-view-context'
Expand Down

0 comments on commit 9fa6a8b

Please sign in to comment.