Skip to content

Commit

Permalink
Disable transition if search is opened by keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
barvian committed Jun 21, 2024
1 parent 7d971e6 commit ac92181
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ export function SearchProvider({ children }) {
const [isOpen, setIsOpen] = useState(false)
const [initialQuery, setInitialQuery] = useState(null)

const onOpen = useCallback(() => {
startViewTransition(() => {
setIsOpen(true)
})
}, [setIsOpen])
const onOpen = useCallback(
(keyboardActivated = false) => {
startViewTransition(() => {
setIsOpen(true)
}, keyboardActivated)
},
[setIsOpen]
)

const onClose = useCallback(() => {
startViewTransition(() => {
setIsOpen(false)
})
}, [setIsOpen])
const onClose = useCallback(
(keyboardActivated = false) => {
startViewTransition(() => {
setIsOpen(false)
}, keyboardActivated)
},
[setIsOpen]
)

const onInput = useCallback(
(e) => {
Expand Down Expand Up @@ -120,7 +126,7 @@ export function SearchProvider({ children }) {
],
}}
placeholder="Search documentation"
onClose={onClose}
onClose={() => void onClose()}
indexName={INDEX_NAME}
apiKey={API_KEY}
appId={APP_ID}
Expand Down Expand Up @@ -231,7 +237,7 @@ export function SearchButton({ children, className = '', ...props }) {
isOpen && 'supports-view-transitions:motion-safe:invisible'
)}
ref={searchButtonRef}
onClick={onOpen}
onClick={() => void onOpen()}
style={{ viewTransitionName: isOpen ? null : 'search-box' }}
{...props}
>
Expand Down Expand Up @@ -300,7 +306,7 @@ function useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }) {
// We check that no other DocSearch modal is showing before opening
// another one.
if (!document.body.classList.contains('DocSearch--active')) {
onOpen()
onOpen(true)
}
}

Expand All @@ -312,7 +318,7 @@ function useDocSearchKeyboardEvents({ isOpen, onOpen, onClose }) {
event.preventDefault()

if (isOpen) {
onClose()
onClose(true)
} else if (!document.body.classList.contains('DocSearch--active')) {
open()
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/startViewTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const motionSafe =
? window.matchMedia('(prefers-reduced-motion: no-preference)')
: undefined

export function startViewTransition(cb) {
if (motionSafe?.matches && typeof document?.startViewTransition === 'function') {
export function startViewTransition(cb, disabled = false) {
if (!disabled && motionSafe?.matches && typeof document?.startViewTransition === 'function') {
document.startViewTransition(() => {
flushSync(cb)
})
Expand Down

0 comments on commit ac92181

Please sign in to comment.