Skip to content

Commit 68f7ecc

Browse files
committed
fix(nuxt): improve module build output path and component ref handling
- Move Nuxt module output to dist/nuxt/ for cleaner build structure - Add support for Vue component refs in attachTo.element - Add null-safe checks for element.style to prevent SSR errors - Refactor interaction functions for better code clarity
1 parent d15604f commit 68f7ecc

4 files changed

Lines changed: 59 additions & 60 deletions

File tree

build.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { defineBuildConfig } from 'unbuild'
22

33
export default defineBuildConfig({
44
entries: ['src/nuxt/module'],
5+
outDir: 'dist/nuxt',
56
externals: ['@nuxt/kit', '@nuxt/schema', 'v-onboarding'],
67
declaration: true,
7-
clean: false,
8+
clean: true,
89
rollup: {
910
emitCJS: false
1011
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
},
3636
"./dist/style.css": "./dist/style.css",
3737
"./nuxt": {
38-
"types": "./dist/module.d.ts",
39-
"import": "./dist/module.mjs"
38+
"types": "./dist/nuxt/module.d.ts",
39+
"import": "./dist/nuxt/module.mjs"
4040
}
4141
},
4242
"scripts": {

src/components/VOnboardingWrapper.vue

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,21 @@ const isActive = computed(() => currentIndex.value >= 0 && currentIndex.value <
3838
const isFirstStep = computed(() => currentIndex.value === 0)
3939
const isLastStep = computed(() => currentIndex.value === props.steps.length - 1)
4040
41-
const getStepOptions = (step?: StepEntity) => {
41+
function getStepOptions(step?: StepEntity): VOnboardingWrapperOptions {
4242
return step ? merge({}, mergedOptions.value, step.options) : mergedOptions.value
4343
}
4444
4545
const POINTER_ATTR = 'data-v-onboarding-pointer-events'
4646
47-
const blockInteraction = (element: HTMLElement | null) => {
48-
if (!element) return
49-
if (element.style.pointerEvents === 'none') return
47+
function setInteraction(element: HTMLElement | null, value: 'none' | 'auto'): void {
48+
if (!element?.style || element.style.pointerEvents === value) return
5049
const current = element.style.pointerEvents
5150
if (current) element.setAttribute(POINTER_ATTR, current)
52-
element.style.pointerEvents = 'none'
51+
element.style.pointerEvents = value
5352
}
5453
55-
const allowInteraction = (element: HTMLElement | null) => {
56-
if (!element) return
57-
if (element.style.pointerEvents === 'auto') return
58-
const current = element.style.pointerEvents
59-
if (current) element.setAttribute(POINTER_ATTR, current)
60-
element.style.pointerEvents = 'auto'
61-
}
62-
63-
const restoreInteraction = (element: HTMLElement | null) => {
64-
if (!element) return
54+
function restoreInteraction(element: HTMLElement | null): void {
55+
if (!element?.style) return
6556
const stored = element.getAttribute(POINTER_ATTR)
6657
if (stored) {
6758
element.style.pointerEvents = stored
@@ -71,7 +62,17 @@ const restoreInteraction = (element: HTMLElement | null) => {
7162
}
7263
}
7364
74-
const runSetup = (step: StepEntity, index: number, direction: Direction) => {
65+
function createHookOptions(step: StepEntity, index: number, direction: Direction) {
66+
return {
67+
index,
68+
step,
69+
direction,
70+
isForward: direction === Direction.FORWARD,
71+
isBackward: direction === Direction.BACKWARD,
72+
}
73+
}
74+
75+
function runSetup(step: StepEntity, index: number, direction: Direction) {
7576
const element = useGetElement(step.attachTo.element) as HTMLElement
7677
const options = getStepOptions(step)
7778
@@ -80,18 +81,14 @@ const runSetup = (step: StepEntity, index: number, direction: Direction) => {
8081
}
8182
8283
if (options?.overlay?.preventOverlayInteraction) {
83-
blockInteraction(document.body)
84-
allowInteraction(element)
84+
setInteraction(document.body, 'none')
85+
setInteraction(element, 'auto')
8586
}
8687
87-
return step.on?.beforeStep?.({
88-
index, step, direction,
89-
isForward: direction === Direction.FORWARD,
90-
isBackward: direction === Direction.BACKWARD,
91-
} as onBeforeStepOptions)
88+
return step.on?.beforeStep?.(createHookOptions(step, index, direction) as onBeforeStepOptions)
9289
}
9390
94-
const runCleanup = (step: StepEntity, index: number, direction: Direction) => {
91+
function runCleanup(step: StepEntity, index: number, direction: Direction) {
9592
const element = useGetElement(step.attachTo.element) as HTMLElement
9693
const options = getStepOptions(step)
9794
@@ -103,14 +100,10 @@ const runCleanup = (step: StepEntity, index: number, direction: Direction) => {
103100
restoreInteraction(element)
104101
}
105102
106-
return step.on?.afterStep?.({
107-
index, step, direction,
108-
isForward: direction === Direction.FORWARD,
109-
isBackward: direction === Direction.BACKWARD,
110-
} as onAfterStepOptions)
103+
return step.on?.afterStep?.(createHookOptions(step, index, direction) as onAfterStepOptions)
111104
}
112105
113-
const goToStep = (target: number | ((current: number) => number)) => {
106+
function goToStep(target: number | ((current: number) => number)): void {
114107
const newIndex = typeof target === 'function' ? target(currentIndex.value) : target
115108
const oldIndex = currentIndex.value
116109
@@ -133,9 +126,11 @@ const goToStep = (target: number | ((current: number) => number)) => {
133126
})()
134127
}
135128
136-
const start = () => goToStep(0)
129+
function start(): void {
130+
goToStep(0)
131+
}
137132
138-
const finish = () => {
133+
function finish(): void {
139134
const step = props.steps[currentIndex.value]
140135
const index = currentIndex.value
141136
@@ -146,11 +141,15 @@ const finish = () => {
146141
if (step) runCleanup(step, index, Direction.FORWARD)
147142
}
148143
149-
const exit = () => emit('exit', currentIndex.value)
144+
function exit(): void {
145+
emit('exit', currentIndex.value)
146+
}
150147
151-
const previous = () => goToStep(i => i - 1)
148+
function previous(): void {
149+
goToStep(i => i - 1)
150+
}
152151
153-
const next = () => {
152+
function next(): void {
154153
if (isLastStep.value) {
155154
finish()
156155
} else {

src/composables/useGetElement.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
import { isRef } from "vue";
22
import type { AttachableElement } from "@/types/lib";
33

4-
/**
5-
* Recursively search for an element within shadow DOMs
6-
*/
74
function querySelectorDeep(selector: string, root: Document | ShadowRoot | Element = document): Element | null {
8-
// First try to find in the current root
9-
const element = root.querySelector(selector);
10-
if (element) return element;
5+
const element = root.querySelector(selector)
6+
if (element) return element
117

12-
// Search in shadow roots of all elements
13-
const allElements = root.querySelectorAll('*');
8+
const allElements = root.querySelectorAll('*')
149
for (const el of allElements) {
1510
if (el.shadowRoot) {
16-
const found = querySelectorDeep(selector, el.shadowRoot);
17-
if (found) return found;
11+
const found = querySelectorDeep(selector, el.shadowRoot)
12+
if (found) return found
1813
}
1914
}
2015

21-
return null;
16+
return null
2217
}
2318

24-
export default function useGetElement(element: AttachableElement) {
19+
export default function useGetElement(element: AttachableElement): Element | null {
2520
if (isRef(element)) {
26-
return element.value ?? null;
21+
const value = element.value
22+
if (value && !(value instanceof Element) && value.$el) {
23+
return value.$el
24+
}
25+
return value ?? null
2726
}
28-
if (typeof element === "string") {
29-
// First try normal querySelector (faster)
30-
const found = document.querySelector(element);
31-
if (found) return found;
3227

33-
// If not found, search within shadow DOMs
34-
return querySelectorDeep(element);
28+
if (typeof element === "string") {
29+
const found = document.querySelector(element)
30+
if (found) return found
31+
return querySelectorDeep(element)
3532
}
36-
else if (typeof element === 'function') {
37-
return element();
33+
34+
if (typeof element === 'function') {
35+
return element()
3836
}
39-
return null;
37+
38+
return null
4039
}

0 commit comments

Comments
 (0)