fix(theme): prevent layout shift in search button key (#2889)

pull/2890/head
Divyansh Singh 2 years ago committed by GitHub
parent 18adc07117
commit 0088434895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,7 @@
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vitepress", "dev": "vitepress dev",
"build": "vitepress build", "build": "vitepress build",
"preview": "vitepress preview" "preview": "vitepress preview"
}, },

@ -34,7 +34,7 @@ useActiveAnchor(container, marker)
<div class="content"> <div class="content">
<div class="outline-marker" ref="marker" /> <div class="outline-marker" ref="marker" />
<div class="outline-title" role="heading">{{ resolveTitle(theme) }}</div> <div class="outline-title" role="heading" aria-level="2">{{ resolveTitle(theme) }}</div>
<nav aria-labelledby="doc-outline-aria-label"> <nav aria-labelledby="doc-outline-aria-label">
<span class="visually-hidden" id="doc-outline-aria-label"> <span class="visually-hidden" id="doc-outline-aria-label">

@ -137,20 +137,11 @@ if (__VP_LOCAL_SEARCH__) {
}) })
} }
const metaKey = ref(`'Meta'`)
onMounted(() => {
// meta key detect (same logic as in @docsearch/js)
metaKey.value = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
? `'⌘'`
: `'Ctrl'`
})
const provider = __ALGOLIA__ ? 'algolia' : __VP_LOCAL_SEARCH__ ? 'local' : '' const provider = __ALGOLIA__ ? 'algolia' : __VP_LOCAL_SEARCH__ ? 'local' : ''
</script> </script>
<template> <template>
<div class="VPNavBarSearch" :style="{ '--vp-meta-key': metaKey }"> <div class="VPNavBarSearch">
<template v-if="provider === 'local'"> <template v-if="provider === 'local'">
<VPLocalSearchBox <VPLocalSearchBox
v-if="showSearch" v-if="showSearch"

@ -194,12 +194,16 @@ defineProps<{
} }
.DocSearch-Button .DocSearch-Button-Key:first-child:after { .DocSearch-Button .DocSearch-Button-Key:first-child:after {
content: var(--vp-meta-key); content: 'Ctrl';
font-size: 12px; font-size: 12px;
letter-spacing: normal; letter-spacing: normal;
color: var(--docsearch-muted-color); color: var(--docsearch-muted-color);
} }
.mac .DocSearch-Button .DocSearch-Button-Key:first-child:after {
content: '\2318';
}
.DocSearch-Button .DocSearch-Button-Key:first-child > * { .DocSearch-Button .DocSearch-Button-Key:first-child > * {
display: none; display: none;
} }

@ -255,19 +255,23 @@ function resolveSiteDataHead(userConfig?: UserConfig): HeadConfig[] {
? userConfig.appearance.initialValue ?? 'auto' ? userConfig.appearance.initialValue ?? 'auto'
: 'auto' : 'auto'
head.push([ head.push(
[
'script', 'script',
{ id: 'check-dark-light' }, { id: 'check-dark-light' },
` `;(() => {
;(() => {
const preference = localStorage.getItem('${APPEARANCE_KEY}') || '${fallbackPreference}' const preference = localStorage.getItem('${APPEARANCE_KEY}') || '${fallbackPreference}'
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
if (!preference || preference === 'auto' ? prefersDark : preference === 'dark') { if (!preference || preference === 'auto' ? prefersDark : preference === 'dark')
document.documentElement.classList.add('dark') document.documentElement.classList.add('dark')
} })()`
})() ],
` [
]) 'script',
{ id: 'check-mac-os' },
`document.documentElement.classList.toggle('mac', /Mac|iPhone|iPod|iPad/i.test(navigator.platform))`
]
)
} }
return head return head

Loading…
Cancel
Save