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

Re-expose el #1230

Merged
merged 2 commits into from
Mar 10, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improve some internal code ([#1221](https://github.com/tailwindlabs/headlessui/pull/1221))
- Don’t drop initial character when searching in Combobox ([#1223](https://github.com/tailwindlabs/headlessui/pull/1223))
- Use `ownerDocument` instead of `document` ([#1158](https://github.com/tailwindlabs/headlessui/pull/1158))
- Re-expose `el` ([#1230](https://github.com/tailwindlabs/headlessui/pull/1230))

### Added

Expand Down
19 changes: 15 additions & 4 deletions packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,12 @@ export let ComboboxButton = defineComponent({
props: {
as: { type: [Object, String], default: 'button' },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useComboboxContext('ComboboxButton')
let id = `headlessui-combobox-button-${useId()}`

expose({ el: api.buttonRef, $el: api.buttonRef })

function handleClick(event: MouseEvent) {
if (api.disabled.value) return
if (api.comboboxState.value === ComboboxStates.Open) {
Expand Down Expand Up @@ -494,11 +496,13 @@ export let ComboboxInput = defineComponent({
emits: {
change: (_value: Event & { target: HTMLInputElement }) => true,
},
setup(props, { emit, attrs, slots }) {
setup(props, { emit, attrs, slots, expose }) {
let api = useComboboxContext('ComboboxInput')
let id = `headlessui-combobox-input-${useId()}`
api.inputPropsRef = computed(() => props)

expose({ el: api.inputRef, $el: api.inputRef })

function handleKeyDown(event: KeyboardEvent) {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
Expand Down Expand Up @@ -620,15 +624,20 @@ export let ComboboxOptions = defineComponent({
unmount: { type: Boolean, default: true },
hold: { type: [Boolean], default: false },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useComboboxContext('ComboboxOptions')
let id = `headlessui-combobox-options-${useId()}`

expose({ el: api.optionsRef, $el: api.optionsRef })

watchEffect(() => {
api.optionsPropsRef.value.static = props.static
})

watchEffect(() => {
api.optionsPropsRef.value.hold = props.hold
})

let usesOpenClosedState = useOpenClosed()
let visible = computed(() => {
if (usesOpenClosedState !== null) {
Expand Down Expand Up @@ -685,11 +694,13 @@ export let ComboboxOption = defineComponent({
value: { type: [Object, String, Number, Boolean] },
disabled: { type: Boolean, default: false },
},
setup(props, { slots, attrs }) {
setup(props, { slots, attrs, expose }) {
let api = useComboboxContext('ComboboxOption')
let id = `headlessui-combobox-option-${useId()}`
let internalOptionRef = ref<HTMLElement | null>(null)

expose({ el: internalOptionRef, $el: internalOptionRef })

let active = computed(() => {
return api.activeOptionIndex.value !== null
? api.options.value[api.activeOptionIndex.value].id === id
Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export let Dialog = defineComponent({
initialFocus: { type: Object as PropType<HTMLElement | null>, default: null },
},
emits: { close: (_close: boolean) => true },
setup(props, { emit, attrs, slots }) {
setup(props, { emit, attrs, slots, expose }) {
let nestedDialogCount = ref(0)

let usesOpenClosedState = useOpenClosed()
Expand All @@ -94,6 +94,8 @@ export let Dialog = defineComponent({
let internalDialogRef = ref<HTMLDivElement | null>(null)
let ownerDocument = computed(() => getOwnerDocument(internalDialogRef))

expose({ el: internalDialogRef, $el: internalDialogRef })

// Validations
let hasOpen = props.open !== Missing || usesOpenClosedState !== null

Expand Down
10 changes: 7 additions & 3 deletions packages/@headlessui-vue/src/components/disclosure/disclosure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ export let DisclosureButton = defineComponent({
as: { type: [Object, String], default: 'button' },
disabled: { type: [Boolean], default: false },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useDisclosureContext('DisclosureButton')

let panelContext = useDisclosurePanelContext()
let isWithinPanel = panelContext === null ? false : panelContext === api.panelId

let internalButtonRef = ref(null)
let internalButtonRef = ref<HTMLButtonElement | null>(null)

expose({ el: internalButtonRef, $el: internalButtonRef })

if (!isWithinPanel) {
watchEffect(() => {
Expand Down Expand Up @@ -240,9 +242,11 @@ export let DisclosurePanel = defineComponent({
static: { type: Boolean, default: false },
unmount: { type: Boolean, default: true },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useDisclosureContext('DisclosurePanel')

expose({ el: api.panel, $el: api.panel })

provide(DisclosurePanelContext, api.panelId)

let usesOpenClosedState = useOpenClosed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export let FocusTrap = defineComponent({
as: { type: [Object, String], default: 'div' },
initialFocus: { type: Object as PropType<HTMLElement | null>, default: null },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let container = ref<HTMLElement | null>(null)

expose({ el: container, $el: container })

let focusTrapOptions = computed(() => ({ initialFocus: ref(props.initialFocus) }))
useFocusTrap(container, FocusTrap.All, focusTrapOptions)

Expand Down
12 changes: 9 additions & 3 deletions packages/@headlessui-vue/src/components/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,12 @@ export let ListboxButton = defineComponent({
props: {
as: { type: [Object, String], default: 'button' },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useListboxContext('ListboxButton')
let id = `headlessui-listbox-button-${useId()}`

expose({ el: api.buttonRef, $el: api.buttonRef })

function handleKeyDown(event: KeyboardEvent) {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
Expand Down Expand Up @@ -450,11 +452,13 @@ export let ListboxOptions = defineComponent({
static: { type: Boolean, default: false },
unmount: { type: Boolean, default: true },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useListboxContext('ListboxOptions')
let id = `headlessui-listbox-options-${useId()}`
let searchDebounce = ref<ReturnType<typeof setTimeout> | null>(null)

expose({ el: api.optionsRef, $el: api.optionsRef })

function handleKeyDown(event: KeyboardEvent) {
if (searchDebounce.value) clearTimeout(searchDebounce.value)

Expand Down Expand Up @@ -572,11 +576,13 @@ export let ListboxOption = defineComponent({
value: { type: [Object, String, Number, Boolean] },
disabled: { type: Boolean, default: false },
},
setup(props, { slots, attrs }) {
setup(props, { slots, attrs, expose }) {
let api = useListboxContext('ListboxOption')
let id = `headlessui-listbox-option-${useId()}`
let internalOptionRef = ref<HTMLElement | null>(null)

expose({ el: internalOptionRef, $el: internalOptionRef })

let active = computed(() => {
return api.activeOptionIndex.value !== null
? api.options.value[api.activeOptionIndex.value].id === id
Expand Down
12 changes: 9 additions & 3 deletions packages/@headlessui-vue/src/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,12 @@ export let MenuButton = defineComponent({
disabled: { type: Boolean, default: false },
as: { type: [Object, String], default: 'button' },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useMenuContext('MenuButton')
let id = `headlessui-menu-button-${useId()}`

expose({ el: api.buttonRef, $el: api.buttonRef })

function handleKeyDown(event: KeyboardEvent) {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
Expand Down Expand Up @@ -330,11 +332,13 @@ export let MenuItems = defineComponent({
static: { type: Boolean, default: false },
unmount: { type: Boolean, default: true },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useMenuContext('MenuItems')
let id = `headlessui-menu-items-${useId()}`
let searchDebounce = ref<ReturnType<typeof setTimeout> | null>(null)

expose({ el: api.itemsRef, $el: api.itemsRef })

useTreeWalker({
container: computed(() => dom(api.itemsRef)),
enabled: computed(() => api.menuState.value === MenuStates.Open),
Expand Down Expand Up @@ -474,11 +478,13 @@ export let MenuItem = defineComponent({
as: { type: [Object, String], default: 'template' },
disabled: { type: Boolean, default: false },
},
setup(props, { slots, attrs }) {
setup(props, { slots, attrs, expose }) {
let api = useMenuContext('MenuItem')
let id = `headlessui-menu-item-${useId()}`
let internalItemRef = ref<HTMLElement | null>(null)

expose({ el: internalItemRef, $el: internalItemRef })

let active = computed(() => {
return api.activeItemIndex.value !== null
? api.items.value[api.activeItemIndex.value].id === id
Expand Down
16 changes: 12 additions & 4 deletions packages/@headlessui-vue/src/components/popover/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ export let Popover = defineComponent({
props: {
as: { type: [Object, String], default: 'div' },
},
setup(props, { slots, attrs }) {
setup(props, { slots, attrs, expose }) {
let buttonId = `headlessui-popover-button-${useId()}`
let panelId = `headlessui-popover-panel-${useId()}`

let internalPopoverRef = ref<HTMLElement | null>(null)

expose({ el: internalPopoverRef, $el: internalPopoverRef })

let popoverState = ref<StateDefinition['popoverState']['value']>(PopoverStates.Closed)
let button = ref<StateDefinition['button']['value']>(null)
let panel = ref<StateDefinition['panel']['value']>(null)
Expand Down Expand Up @@ -217,10 +219,12 @@ export let PopoverButton = defineComponent({
as: { type: [Object, String], default: 'button' },
disabled: { type: [Boolean], default: false },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = usePopoverContext('PopoverButton')
let ownerDocument = computed(() => getOwnerDocument(api.button))

expose({ el: api.button, $el: api.button })

let groupContext = usePopoverGroupContext()
let closeOthers = groupContext?.closeOthers

Expand Down Expand Up @@ -462,11 +466,13 @@ export let PopoverPanel = defineComponent({
unmount: { type: Boolean, default: true },
focus: { type: Boolean, default: false },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let { focus } = props
let api = usePopoverContext('PopoverPanel')
let ownerDocument = computed(() => getOwnerDocument(api.panel))

expose({ el: api.panel, $el: api.panel })

provide(PopoverPanelContext, api.panelId)

onUnmounted(() => {
Expand Down Expand Up @@ -599,11 +605,13 @@ export let PopoverGroup = defineComponent({
props: {
as: { type: [Object, String], default: 'div' },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let groupRef = ref<HTMLElement | null>(null)
let popovers = ref<PopoverRegisterBag[]>([])
let ownerDocument = computed(() => getOwnerDocument(groupRef))

expose({ el: groupRef, $el: groupRef })

function unregisterPopover(registerBag: PopoverRegisterBag) {
let idx = popovers.value.indexOf(registerBag)
if (idx !== -1) popovers.value.splice(idx, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ export let RadioGroup = defineComponent({
modelValue: { type: [Object, String, Number, Boolean] },
name: { type: String, optional: true },
},
setup(props, { emit, attrs, slots }) {
setup(props, { emit, attrs, slots, expose }) {
let radioGroupRef = ref<HTMLElement | null>(null)
let options = ref<StateDefinition['options']['value']>([])
let labelledby = useLabels({ name: 'RadioGroupLabel' })
let describedby = useDescriptions({ name: 'RadioGroupDescription' })

expose({ el: radioGroupRef, $el: radioGroupRef })

let value = computed(() => props.modelValue)

let api = {
Expand Down Expand Up @@ -247,7 +249,7 @@ export let RadioGroupOption = defineComponent({
value: { type: [Object, String, Number, Boolean] },
disabled: { type: Boolean, default: false },
},
setup(props, { attrs, slots }) {
setup(props, { attrs, slots, expose }) {
let api = useRadioGroupContext('RadioGroupOption')
let id = `headlessui-radiogroup-option-${useId()}`
let labelledby = useLabels({ name: 'RadioGroupLabel' })
Expand All @@ -257,6 +259,8 @@ export let RadioGroupOption = defineComponent({
let propsRef = computed(() => ({ value: props.value, disabled: props.disabled }))
let state = ref(OptionState.Empty)

expose({ el: optionRef, $el: optionRef })

onMounted(() => api.registerOption({ id, element: optionRef, propsRef }))
onUnmounted(() => api.unregisterOption(id))

Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/src/components/switch/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export let Switch = defineComponent({
value: { type: String, optional: true },
},

setup(props, { emit, attrs, slots }) {
setup(props, { emit, attrs, slots, expose }) {
let api = inject(GroupContext, null)
let id = `headlessui-switch-${useId()}`

Expand All @@ -85,6 +85,8 @@ export let Switch = defineComponent({
switchRef
)

expose({ el: switchRef, $el: switchRef })

function handleClick(event: MouseEvent) {
event.preventDefault()
toggle()
Expand Down
Loading