-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: replace `ClientOnly` component with `clientOnly` function with TS support #67, #82 BREAKING CHANGE: `ClientOnly` component is removed, please use the new `clientOnly` function to create components that only load and render on client side * chore: some tests to be removed before merge * minor * simplify slots handling? * always require argument to be a function returning a promise? * fix: call alternate fallback slot function * refactor slots definition * swallow error when loading component and expose it as prop for fallback slot * fix slots typing * cleanup examples * expose attrs to fallback slots so styles can be applied if required * minor refactor * let's change the docs URL after vike-react also makes the migration to a clientOnly() function * small tweak of example - move timer to the slow loading component example * more `clientOnly` under helpers --------- Co-authored-by: Romuald Brillout <[email protected]>
- Loading branch information
Showing
8 changed files
with
169 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<template> | ||
<button type="button" @click="state.status = state.status !== true"> | ||
<template v-if="state.status !== null"> | ||
<slot name="prefix">State is </slot> | ||
<slot :status="state.status">{{ state.status ? 'On' : 'Off' }}</slot> | ||
</template> | ||
<slot v-else name="fallback">No state</slot> | ||
</button> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { reactive, watch } from 'vue' | ||
const props = withDefaults( | ||
defineProps<{ | ||
status?: boolean | null | ||
}>(), | ||
{ | ||
status: false, | ||
}, | ||
) | ||
const emit = defineEmits<{ | ||
toggle: [value: boolean | null] | ||
}>() | ||
defineSlots<{ | ||
default: { status: boolean } | ||
prefix: {} | ||
fallback: {} | ||
}>() | ||
const state = reactive({ status: false as boolean | null }) | ||
watch( | ||
() => props.status, | ||
(val) => { | ||
state.status = val | ||
}, | ||
{ immediate: true }, | ||
) | ||
watch( | ||
() => state.status, | ||
(val) => { | ||
emit('toggle', val) | ||
}, | ||
) | ||
</script> |
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 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,49 @@ | ||
export { clientOnly } | ||
|
||
import { h, nextTick, shallowRef, defineComponent, onBeforeMount } from 'vue' | ||
import type { Component, SlotsType } from 'vue' | ||
|
||
function clientOnly<ComponentLoaded extends Component>( | ||
load: () => Promise<ComponentLoaded | { default: ComponentLoaded }>, | ||
) { | ||
const componentWrapper = defineComponent({ | ||
inheritAttrs: false, | ||
|
||
setup(_, { attrs, slots }) { | ||
const componentLoaded = shallowRef<ComponentLoaded | null>(null) | ||
const error = shallowRef<unknown>(null) | ||
|
||
onBeforeMount(async () => { | ||
try { | ||
const ret = await load() | ||
componentLoaded.value = 'default' in ret ? ret.default : ret | ||
} catch (e) { | ||
console.error('Error while loading clientOnly() component:', e) | ||
// wait for nextTick to avoid hydration errors | ||
nextTick(() => { | ||
error.value = e | ||
}) | ||
} | ||
}) | ||
|
||
return () => { | ||
if (componentLoaded.value !== null) { | ||
return h(componentLoaded.value, attrs, slots) | ||
} | ||
if (slots['client-only-fallback']) { | ||
return slots['client-only-fallback']({ error: error.value, attrs }) | ||
} | ||
// if the user doesn't want clientOnly() to use <template #fallback> then he should define a (empty) <template #client-only-fallback> | ||
if (slots['fallback']) { | ||
return slots['fallback']({ error: error.value, attrs }) | ||
} | ||
} | ||
}, | ||
|
||
slots: {} as SlotsType<{ | ||
fallback: { error: unknown; attrs: Record<string, any> } | ||
'client-only-fallback': { error: unknown; attrs: Record<string, any> } | ||
}>, | ||
}) | ||
return componentWrapper as typeof componentWrapper & ComponentLoaded | ||
} |
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