-
-
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
- Loading branch information
Showing
8 changed files
with
177 additions
and
18 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export { clientOnly } | ||
|
||
import { h, nextTick, shallowRef, defineComponent, onBeforeMount } from 'vue' | ||
import type { Component, SlotsType } from 'vue' | ||
|
||
/** | ||
* Load and render a component only on the client-side. | ||
* | ||
* https://vike.dev/clientOnly | ||
*/ | ||
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