forked from vikejs/vike-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace
ClientOnly
component with clientOnly
function with …
…TS support vikejs#67, vikejs#82 BREAKING CHANGE: `ClientOnly` component is removed, please use the new `clientOnly` function to create components that only load and render on client side
- Loading branch information
Showing
6 changed files
with
62 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { h, shallowRef, defineComponent, onBeforeMount } from 'vue' | ||
import type { Component, SlotsType } from 'vue' | ||
|
||
type MaybePromise<T> = T | Promise<T> | ||
type ComponentResolved<T> = MaybePromise<T | { default: T; }> | ||
|
||
type ClientOnlySlots = { | ||
fallback?: {}; | ||
'client-only-fallback'?: {}; | ||
} | ||
|
||
export function clientOnly<T extends Component>( | ||
source: ComponentResolved<T> | (() => ComponentResolved<T>), | ||
) { | ||
const clientOnlyComponent = defineComponent({ | ||
inheritAttrs: false, | ||
|
||
setup(_, { attrs, slots }) { | ||
const resolvedComp = shallowRef<T | null>(null) | ||
|
||
onBeforeMount(() => { | ||
const loader = source instanceof Function ? source : () => source | ||
Promise.resolve(loader()) | ||
.then((component) => { | ||
resolvedComp.value = 'default' in component ? component.default : component | ||
}) | ||
.catch((e) => { | ||
console.error('Component loading failed:', e) | ||
throw e | ||
}) | ||
}) | ||
|
||
const cleanSlots = (slots: ClientOnlySlots) => { | ||
const cleaned = { ...slots } | ||
if (slots[ 'client-only-fallback' ]) { | ||
delete cleaned['client-only-fallback'] | ||
} else { | ||
delete cleaned.fallback | ||
} | ||
return cleaned | ||
} | ||
|
||
return () => | ||
resolvedComp.value !== null | ||
? h(resolvedComp.value, attrs, cleanSlots(slots as ClientOnlySlots)) | ||
: slots['client-only-fallback']?.() | ||
}, | ||
|
||
slots: Object as SlotsType<ClientOnlySlots>, | ||
}) | ||
|
||
return clientOnlyComponent as typeof clientOnlyComponent & T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters