-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathprepare.js
80 lines (64 loc) · 2.3 KB
/
prepare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { addCleanupTask } from './cleanup.js'
import * as MountLegacy from './mount-legacy.js'
import * as MountModern from './mount-modern.svelte.js'
const ALLOWED_OPTIONS = MountModern.IS_MODERN_SVELTE
? MountModern.ALLOWED_OPTIONS
: MountLegacy.ALLOWED_OPTIONS
/** An error thrown for incorrect options and clashes between props and Svelte options. */
class UnknownSvelteOptionsError extends TypeError {
constructor(unknownOptions) {
super(`Unknown options.
Unknown: [ ${unknownOptions.join(', ')} ]
Allowed: [ ${ALLOWED_OPTIONS.join(', ')} ]
To pass both Svelte options and props to a component,
or to use props that share a name with a Svelte option,
you must place all your props under the \`props\` key:
render(Component, { props: { /** props here **/ } })
`)
this.name = 'UnknownSvelteOptionsError'
}
}
/**
* Prepare DOM elements for rendering.
*
* @template {import('./types.js').Component} C
* @param {import('./types.js').PropsOrMountOptions<C>} propsOrOptions
* @param {{ baseElement?: HTMLElement }} renderOptions
* @returns {{
* baseElement: HTMLElement
* target: HTMLElement
* mountOptions: import('./types.js').MountOptions<C>
* }}
*/
const prepare = (propsOrOptions = {}, renderOptions = {}) => {
const mountOptions = validateMountOptions(propsOrOptions)
const baseElement =
renderOptions.baseElement ?? mountOptions.target ?? document.body
const target =
mountOptions.target ??
baseElement.appendChild(document.createElement('div'))
addCleanupTask(() => {
if (target.parentNode === document.body) {
document.body.removeChild(target)
}
})
return { baseElement, target, mountOptions: { ...mountOptions, target } }
}
/** Prevent incorrect options and clashes between props and Svelte options. */
const validateMountOptions = (options) => {
const isProps = !Object.keys(options).some((option) =>
ALLOWED_OPTIONS.includes(option)
)
if (isProps) {
return { props: options }
}
// Check if any props and Svelte options were accidentally mixed.
const unknownOptions = Object.keys(options).filter(
(option) => !ALLOWED_OPTIONS.includes(option)
)
if (unknownOptions.length > 0) {
throw new UnknownSvelteOptionsError(unknownOptions)
}
return options
}
export { prepare, UnknownSvelteOptionsError }