1+ import { addCleanupTask } from './cleanup.js'
2+ import * as MountLegacy from './mount-legacy.js'
3+ import * as MountModern from './mount-modern.svelte.js'
4+
5+ const ALLOWED_OPTIONS = MountModern . IS_MODERN_SVELTE
6+ ? MountModern . ALLOWED_OPTIONS
7+ : MountLegacy . ALLOWED_OPTIONS
8+
9+ /** An error thrown for incorrect options and clashes between props and Svelte options. */
110class UnknownSvelteOptionsError extends TypeError {
2- constructor ( unknownOptions , allowedOptions ) {
11+ constructor ( unknownOptions ) {
312 super ( `Unknown options.
413
514 Unknown: [ ${ unknownOptions . join ( ', ' ) } ]
6- Allowed: [ ${ allowedOptions . join ( ', ' ) } ]
15+ Allowed: [ ${ ALLOWED_OPTIONS . join ( ', ' ) } ]
716
817 To pass both Svelte options and props to a component,
918 or to use props that share a name with a Svelte option,
@@ -15,9 +24,41 @@ class UnknownSvelteOptionsError extends TypeError {
1524 }
1625}
1726
18- const createValidateOptions = ( allowedOptions ) => ( options ) => {
27+ /**
28+ * Prepare DOM elements for rendering.
29+ *
30+ * @template {import('./types.js').Component} C
31+ * @param {import('./types.js').PropsOrMountOptions<C> } propsOrOptions
32+ * @param {{ baseElement?: HTMLElement } } renderOptions
33+ * @returns {{
34+ * baseElement: HTMLElement
35+ * target: HTMLElement
36+ * mountOptions: import('./types.js').MountOptions<C>
37+ * }}
38+ */
39+ const prepare = ( propsOrOptions = { } , renderOptions = { } ) => {
40+ const mountOptions = validateMountOptions ( propsOrOptions )
41+
42+ const baseElement =
43+ renderOptions . baseElement ?? mountOptions . target ?? document . body
44+
45+ const target =
46+ mountOptions . target ??
47+ baseElement . appendChild ( document . createElement ( 'div' ) )
48+
49+ addCleanupTask ( ( ) => {
50+ if ( target . parentNode === document . body ) {
51+ document . body . removeChild ( target )
52+ }
53+ } )
54+
55+ return { baseElement, target, mountOptions : { ...mountOptions , target } }
56+ }
57+
58+ /** Prevent incorrect options and clashes between props and Svelte options. */
59+ const validateMountOptions = ( options ) => {
1960 const isProps = ! Object . keys ( options ) . some ( ( option ) =>
20- allowedOptions . includes ( option )
61+ ALLOWED_OPTIONS . includes ( option )
2162 )
2263
2364 if ( isProps ) {
@@ -26,14 +67,14 @@ const createValidateOptions = (allowedOptions) => (options) => {
2667
2768 // Check if any props and Svelte options were accidentally mixed.
2869 const unknownOptions = Object . keys ( options ) . filter (
29- ( option ) => ! allowedOptions . includes ( option )
70+ ( option ) => ! ALLOWED_OPTIONS . includes ( option )
3071 )
3172
3273 if ( unknownOptions . length > 0 ) {
33- throw new UnknownSvelteOptionsError ( unknownOptions , allowedOptions )
74+ throw new UnknownSvelteOptionsError ( unknownOptions )
3475 }
3576
3677 return options
3778}
3879
39- export { createValidateOptions , UnknownSvelteOptionsError }
80+ export { prepare , UnknownSvelteOptionsError }
0 commit comments