Skip to content

Commit 056b311

Browse files
authored
Expose --input-width and --button-width CSS variables on the ComboboxOptions component (#3057)
* add both `--input-width` and `--button-width` to `ComboboxOptions` * use `--input-width` and `--button-width` in combobox countries example * update changelog
1 parent 000e0c0 commit 056b311

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
### Added
2929

3030
- Accept optional `strategy` for the `anchor` prop ([#3034](https://github.com/tailwindlabs/headlessui/pull/3034))
31+
- Expose `--input-width` and `--button-width` CSS variables on the `ComboboxOptions` component ([#3057](https://github.com/tailwindlabs/headlessui/pull/3057))
3132

3233
## [2.0.0-alpha.4] - 2024-01-03
3334

packages/@headlessui-react/src/components/combobox/combobox.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import React, {
1313
useReducer,
1414
useRef,
1515
useState,
16+
type CSSProperties,
1617
type ElementType,
1718
type MutableRefObject,
1819
type FocusEvent as ReactFocusEvent,
@@ -24,6 +25,7 @@ import { useActivePress } from '../../hooks/use-active-press'
2425
import { useByComparator, type ByComparator } from '../../hooks/use-by-comparator'
2526
import { useControllable } from '../../hooks/use-controllable'
2627
import { useDisposables } from '../../hooks/use-disposables'
28+
import { useElementSize } from '../../hooks/use-element-size'
2729
import { useEvent } from '../../hooks/use-event'
2830
import { useId } from '../../hooks/use-id'
2931
import { useIsoMorphicEffect } from '../../hooks/use-iso-morphic-effect'
@@ -1604,7 +1606,11 @@ function OptionsFn<TTag extends ElementType = typeof DEFAULT_OPTIONS_TAG>(
16041606
'aria-multiselectable': data.mode === ValueMode.Multi ? true : undefined,
16051607
id,
16061608
ref: optionsRef,
1607-
...(style ? { style } : {}),
1609+
style: {
1610+
...style,
1611+
'--input-width': useElementSize(data.inputRef, true).width,
1612+
'--button-width': useElementSize(data.buttonRef, true).width,
1613+
} as CSSProperties,
16081614
})
16091615

16101616
// Map the children in a scrollable container when virtualization is enabled

playgrounds/react/pages/combobox/combobox-countries.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export default function Home() {
7272
</Combobox.Button>
7373
</span>
7474

75-
<div className="absolute mt-1 w-full rounded-md bg-white shadow-lg">
76-
<Combobox.Options className="shadow-xs max-h-60 overflow-auto rounded-md py-1 text-base leading-6 focus:outline-none sm:text-sm sm:leading-5">
75+
<div className="absolute mt-1 rounded-md bg-white shadow-lg">
76+
<Combobox.Options className="shadow-xs max-h-60 w-[calc(var(--input-width)+var(--button-width))] overflow-auto rounded-md py-1 text-base leading-6 focus:outline-none sm:text-sm sm:leading-5">
7777
{countries.map((country) => (
7878
<Combobox.Option
7979
key={country}

0 commit comments

Comments
 (0)