Skip to content

Commit dafcc2d

Browse files
authored
Only render the FocusSentinel if required in the Tabs component (#1493)
* only render the `FocusSentinel` if required Fixes: #1491 * update changelog
1 parent e819c0a commit dafcc2d

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Allow to override the `type` on the `ComboboxInput` ([#1476](https://github.com/tailwindlabs/headlessui/pull/1476))
1313
- Ensure the the `<PopoverPanel focus>` closes correctly ([#1477](https://github.com/tailwindlabs/headlessui/pull/1477))
14+
- Only render the `FocusSentinel` if required in the `Tabs` component ([#1493](https://github.com/tailwindlabs/headlessui/pull/1493))
1415

1516
### Added
1617

@@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2223

2324
- Allow to override the `type` on the `Combobox.Input` ([#1476](https://github.com/tailwindlabs/headlessui/pull/1476))
2425
- Ensure the the `<Popover.Panel focus>` closes correctly ([#1477](https://github.com/tailwindlabs/headlessui/pull/1477))
26+
- Only render the `FocusSentinel` if required in the `Tabs` component ([#1493](https://github.com/tailwindlabs/headlessui/pull/1493))
2527

2628
### Added
2729

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ function useData(component: string) {
144144
}
145145
return context
146146
}
147+
type _Data = ReturnType<typeof useData>
147148

148149
let TabsActionsContext = createContext<{
149150
registerTab(tab: MutableRefObject<HTMLElement | null>): () => void
@@ -162,6 +163,7 @@ function useActions(component: string) {
162163
}
163164
return context
164165
}
166+
type _Actions = ReturnType<typeof useActions>
165167

166168
function stateReducer(state: StateDefinition, action: Actions) {
167169
return match(action.type, reducers, state, action)
@@ -205,13 +207,13 @@ let Tabs = forwardRefWithAs(function Tabs<TTag extends ElementType = typeof DEFA
205207
let onChangeRef = useLatestValue(onChange || (() => {}))
206208
let stableTabsRef = useLatestValue(state.tabs)
207209

208-
let tabsData = useMemo<ContextType<typeof TabsDataContext>>(
210+
let tabsData = useMemo<_Data>(
209211
() => ({ orientation, activation, ...state }),
210212
[orientation, activation, state]
211213
)
212214

213215
let lastChangedIndex = useLatestValue(state.selectedIndex)
214-
let tabsActions: ContextType<typeof TabsActionsContext> = useMemo(
216+
let tabsActions: _Actions = useMemo(
215217
() => ({
216218
registerTab(tab) {
217219
dispatch({ type: ActionTypes.RegisterTab, tab })
@@ -245,18 +247,20 @@ let Tabs = forwardRefWithAs(function Tabs<TTag extends ElementType = typeof DEFA
245247
<TabsSSRContext.Provider value={SSRCounter}>
246248
<TabsActionsContext.Provider value={tabsActions}>
247249
<TabsDataContext.Provider value={tabsData}>
248-
<FocusSentinel
249-
onFocus={() => {
250-
for (let tab of stableTabsRef.current) {
251-
if (tab.current?.tabIndex === 0) {
252-
tab.current?.focus()
253-
return true
250+
{tabsData.tabs.length <= 0 && (
251+
<FocusSentinel
252+
onFocus={() => {
253+
for (let tab of stableTabsRef.current) {
254+
if (tab.current?.tabIndex === 0) {
255+
tab.current?.focus()
256+
return true
257+
}
254258
}
255-
}
256259

257-
return false
258-
}}
259-
/>
260+
return false
261+
}}
262+
/>
263+
)}
260264
{render({
261265
ourProps,
262266
theirProps,

packages/@headlessui-vue/src/components/tabs/tabs.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,20 @@ export let TabGroup = defineComponent({
139139
let slot = { selectedIndex: selectedIndex.value }
140140

141141
return h(Fragment, [
142-
h(FocusSentinel, {
143-
onFocus: () => {
144-
for (let tab of tabs.value) {
145-
let el = dom(tab)
146-
if (el?.tabIndex === 0) {
147-
el.focus()
148-
return true
142+
tabs.value.length <= 0 &&
143+
h(FocusSentinel, {
144+
onFocus: () => {
145+
for (let tab of tabs.value) {
146+
let el = dom(tab)
147+
if (el?.tabIndex === 0) {
148+
el.focus()
149+
return true
150+
}
149151
}
150-
}
151152

152-
return false
153-
},
154-
}),
153+
return false
154+
},
155+
}),
155156
render({
156157
props: {
157158
...attrs,

0 commit comments

Comments
 (0)