Skip to content

Commit 6cd7208

Browse files
improve demo mode
1 parent c48fbf2 commit 6cd7208

File tree

1 file changed

+23
-1
lines changed
  • packages/@headlessui-react/src/components/combobox

1 file changed

+23
-1
lines changed

Diff for: packages/@headlessui-react/src/components/combobox/combobox.tsx

+23-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ interface StateDefinition {
5353
comboboxPropsRef: MutableRefObject<{
5454
value: unknown
5555
onChange(value: unknown): void
56+
__demoMode: boolean
5657
}>
5758
inputPropsRef: MutableRefObject<{
5859
displayValue?(item: unknown): string
@@ -146,7 +147,7 @@ let reducers: {
146147
(a, z) => orderMap[a.id] - orderMap[z.id]
147148
)
148149

149-
return {
150+
let nextState = {
150151
...state,
151152
options,
152153
activeOptionIndex: (() => {
@@ -158,6 +159,15 @@ let reducers: {
158159
return options.indexOf(currentActiveOption)
159160
})(),
160161
}
162+
163+
if (
164+
state.comboboxPropsRef.current.__demoMode &&
165+
state.comboboxPropsRef.current.value === undefined
166+
) {
167+
nextState.activeOptionIndex = 0
168+
}
169+
170+
return nextState
161171
},
162172
[ActionTypes.UnregisterOption]: (state, action) => {
163173
let nextOptions = state.options.slice()
@@ -243,6 +253,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
243253
let comboboxPropsRef = useRef<StateDefinition['comboboxPropsRef']['current']>({
244254
value,
245255
onChange,
256+
__demoMode,
246257
})
247258
let optionsPropsRef = useRef<StateDefinition['optionsPropsRef']['current']>({
248259
static: false,
@@ -853,9 +864,20 @@ function Option<
853864
dispatch({ type: ActionTypes.GoToOption, focus: Focus.Specific, id })
854865
}, [state.comboboxState, selected, id])
855866

867+
let enableScrollIntoView = useRef(state.comboboxPropsRef.current.__demoMode ? false : true)
868+
useIsoMorphicEffect(() => {
869+
if (!state.comboboxPropsRef.current.__demoMode) return
870+
let d = disposables()
871+
d.requestAnimationFrame(() => {
872+
enableScrollIntoView.current = true
873+
})
874+
return d.dispose
875+
}, [])
876+
856877
useIsoMorphicEffect(() => {
857878
if (state.comboboxState !== ComboboxStates.Open) return
858879
if (!active) return
880+
if (!enableScrollIntoView.current) return
859881
let d = disposables()
860882
d.requestAnimationFrame(() => {
861883
document.getElementById(id)?.scrollIntoView?.({ block: 'nearest' })

0 commit comments

Comments
 (0)