Skip to content

Commit 678ab67

Browse files
authored
fix(anchor): [anchor] fixed the bug that querySelect cannot select sp… (#2619)
* fix(anchor): [anchor] fixed the bug that querySelect cannot select special id * fix(anchor): [anchor] fixed the bug that querySelect cannot select special id
1 parent e81f11c commit 678ab67

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

packages/renderless/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opentiny/vue-renderless",
3-
"version": "3.20.1",
3+
"version": "3.20.2",
44
"private": true,
55
"description": "An enterprise-class UI component library, support both Vue.js 2 and Vue.js 3, as well as PC and mobile.",
66
"author": "OpenTiny Team",

packages/renderless/src/anchor/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import type { IAnchorRenderlessParams, IAnchorLinkItem } from '@/types'
1313
import { addClass, removeClass } from '../common/deps/dom'
1414

15+
const getEleMentBySelect = (parent, selector) =>
16+
selector?.startsWith('#') ? document.getElementById('selector') : parent.querySelector(selector)
17+
1518
export const setFixAnchor =
1619
({ vm, props }: Pick<IAnchorRenderlessParams, 'vm' | 'props'>) =>
1720
() => {
@@ -25,7 +28,7 @@ export const setFixAnchor =
2528
const setMarkClass = ({ state, props }: Pick<IAnchorRenderlessParams, 'state' | 'props'>) => {
2629
const { scrollContainer } = state
2730
const { markClass } = props
28-
const activeContentEl = scrollContainer.querySelector(`${state.currentLink}`)
31+
const activeContentEl = getEleMentBySelect(scrollContainer, `${state.currentLink}`)
2932
if (markClass) {
3033
addClass(activeContentEl, markClass)
3134
setTimeout(() => {
@@ -95,7 +98,7 @@ const addObserver = ({ props, state }: Pick<IAnchorRenderlessParams, 'props' | '
9598
list.forEach((item) => {
9699
const link = item.link
97100
expandLink[link] = item
98-
const linkEl = document.querySelector(link)
101+
const linkEl = getEleMentBySelect(document, link)
99102
linkEl && intersectionObserver.observe(linkEl)
100103
if (item.children) {
101104
observer(item.children)
@@ -127,13 +130,13 @@ const setChildOffsetTop = ({ state, props }: Pick<IAnchorRenderlessParams, 'stat
127130
if (!props.links?.length) {
128131
return
129132
}
130-
state.childOffsetTop = document.querySelector(props.links[0].link)?.offsetTop || 0
133+
state.childOffsetTop = getEleMentBySelect(document, props.links[0].link)?.offsetTop || 0
131134
}
132135

133136
export const getContainer =
134137
({ props }: Pick<IAnchorRenderlessParams, 'props'>) =>
135138
(): Element =>
136-
(props.containerId && document.querySelector(props.containerId)) || document.body
139+
(props.containerId && getEleMentBySelect(document, props.containerId)) || document.body
137140

138141
export const mounted =
139142
({ state, api, props, nextTick }: Pick<IAnchorRenderlessParams, 'state' | 'api' | 'props' | 'nextTick'>) =>
@@ -234,7 +237,7 @@ export const linkClick =
234237
setMarkClass({ state, props })
235238

236239
if (scrollContainer && scrollContainer !== document.body && !isChangeHash) {
237-
const linkEl = scrollContainer.querySelector(item.link) as HTMLElement
240+
const linkEl = getEleMentBySelect(scrollContainer, item.link) as HTMLElement
238241
const top =
239242
linkEl?.getBoundingClientRect().top -
240243
scrollContainer.getBoundingClientRect().top +

0 commit comments

Comments
 (0)