Skip to content
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

Expose the --button-width CSS variable on the PopoverPanel component #3058

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ let act = _act as unknown as <T>(fn: () => T) => PromiseLike<T>

jest.mock('../../hooks/use-id')

// @ts-expect-error
global.ResizeObserver = class FakeResizeObserver {
observe() {}
disconnect() {}
}

afterAll(() => jest.restoreAllMocks())

function nextFrame() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import React, {
type Ref,
} from 'react'
import { useActivePress } from '../../hooks/use-active-press'
import { useElementSize } from '../../hooks/use-element-size'
import { useEvent } from '../../hooks/use-event'
import { useEventListener } from '../../hooks/use-event-listener'
import { useId } from '../../hooks/use-id'
Expand Down Expand Up @@ -913,7 +914,10 @@ function PanelFn<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
}
: undefined,
tabIndex: -1,
...(style ? { style } : {}),
style: {
...style,
'--button-width': useElementSize(state.button, true).width,
} as React.CSSProperties,
})

let direction = useTabDirection()
Expand Down
11 changes: 7 additions & 4 deletions packages/@headlessui-react/src/hooks/use-element-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ function computeSize(element: HTMLElement | null) {
return { width, height }
}

export function useElementSize(ref: React.MutableRefObject<HTMLElement | null>, unit = false) {
let [size, setSize] = useState(() => computeSize(ref.current))
export function useElementSize(
ref: React.MutableRefObject<HTMLElement | null> | HTMLElement | null,
unit = false
) {
let element = ref === null ? null : 'current' in ref ? ref.current : ref
let [size, setSize] = useState(() => computeSize(element))

useIsoMorphicEffect(() => {
let element = ref.current
if (!element) return

let observer = new ResizeObserver(() => {
Expand All @@ -23,7 +26,7 @@ export function useElementSize(ref: React.MutableRefObject<HTMLElement | null>,
return () => {
observer.disconnect()
}
}, [ref])
}, [element])

if (unit) {
return {
Expand Down
Loading