Skip to content

Commit

Permalink
feat(ui): new composable - useHydration()
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Mar 11, 2024
1 parent 0299efa commit 05ec5b6
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 28 deletions.
10 changes: 10 additions & 0 deletions docs/src/assets/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,11 @@ export default [
name: 'useMeta',
path: 'use-meta'
},
{
name: 'useHydration',
badge: 'new',
path: 'use-hydration'
},
{
name: 'useRenderCache',
badge: 'new',
Expand All @@ -676,6 +681,11 @@ export default [
name: 'useTimeout',
badge: 'new',
path: 'use-timeout'
},
{
name: 'useSplitAttrs',
badge: 'new',
path: 'use-split-attrs'
}
]
},
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/vue-components/no-ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ keys: QNoSsr
related:
- /quasar-cli-vite/developing-ssr/introduction
- /quasar-cli-webpack/developing-ssr/introduction
- /vue-composables/use-hydration
---
The QNoSsr component makes sense only if you are creating a SSR website/app.

Expand Down
52 changes: 52 additions & 0 deletions docs/src/pages/vue-composables/use-hydration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: useHydration composable
desc: What is useHydration() composable and how you can use it
keys: useHydration
badge: Quasar v2.15+
related:
- /vue-components/no-ssr
---

The useHydration composable is useful when you build for SSR (but can be used for non SSR builds as well). It is a lower level util of the [QNoSsr](/vue-components/no-ssr) component.

## Syntax

```js
import { useHydration } from 'quasar'

setup () {
const { isHydrated } = useHydration()
}
```

```js
function useHydration(): {
isHydrated: Ref<boolean>;
};
```

## Example

```html
<template>
<div>
<div v-if="isHydrated">
Gets rendered only after hydration.
</div>
</div>
</template>

<script>
import { useHydration } from 'quasar'
export default {
setup () {
const { isHydrated } = useHydration()
return {
isHydrated
}
}
}
</script>
```
2 changes: 1 addition & 1 deletion docs/src/pages/vue-composables/use-render-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ setup () {

```js
function useRenderCache(): {
getCache: <T = any>(key: string, defaultValue?: T | (() => T)) => T | undefined;
getCache: <T = any>(key: string, defaultValue?: T | (() => T)) => T;
setCache: <T = any>(key: string, value: T) => void;
hasCache: (key: string) => boolean;
clearCache: (key?: string) => void;
Expand Down
6 changes: 4 additions & 2 deletions docs/src/pages/vue-composables/use-split-attrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ setup () {
```

```js
import { Ref } from 'vue'

function useSplitAttrs(): {
attributes: Ref<Record<string, any>>;
listeners: Ref<Record<string, any>>;
attributes: Ref<Record<string, string | null | undefined>>;
listeners: Ref<Record<string, (...args: any[]) => any>>;
};
```

Expand Down
6 changes: 3 additions & 3 deletions ui/src/components/no-ssr/QNoSsr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h } from 'vue'

import useCanRender from '../../composables/private/use-can-render.js'
import useHydration from '../../composables/use-hydration.js'

import { createComponent } from '../../utils/private/create.js'
import { hSlot } from '../../utils/private/render.js'
Expand All @@ -18,10 +18,10 @@ export default createComponent({
},

setup (props, { slots }) {
const canRender = useCanRender()
const { isHydrated } = useHydration()

return () => {
if (canRender.value === true) {
if (isHydrated.value === true) {
const node = hSlot(slots.default)
return node === void 0
? node
Expand Down
6 changes: 3 additions & 3 deletions ui/src/components/resize-observer/QResizeObserver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, onMounted, onBeforeUnmount, getCurrentInstance, nextTick } from 'vue'

import useCanRender from '../../composables/private/use-can-render.js'
import useHydration from '../../composables/use-hydration.js'

import { createComponent } from '../../utils/private/create.js'
import { listenOpts, noop } from '../../utils/event.js'
Expand Down Expand Up @@ -95,7 +95,7 @@ export default createComponent({
return noop
}
else { // no observer, so fallback to old iframe method
const canRender = useCanRender()
const { isHydrated } = useHydration()

let curDocView

Expand Down Expand Up @@ -134,7 +134,7 @@ export default createComponent({
onBeforeUnmount(cleanup)

return () => {
if (canRender.value === true) {
if (isHydrated.value === true) {
return h('object', {
class: 'q--avoid-card-border',
style: resizeProps.style,
Expand Down
4 changes: 4 additions & 0 deletions ui/src/composables.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import useDialogPluginComponent from './composables/use-dialog-plugin-component.
import useFormChild from './composables/use-form-child.js'
import useMeta from './composables/use-meta.js'
import useQuasar from './composables/use-quasar.js'

import useHydration from './composables/use-hydration.js'
import useRenderCache from './composables/use-render-cache.js'
import useSplitAttrs from './composables/use-split-attrs.js'
import useTick from './composables/use-tick.js'
Expand All @@ -12,6 +14,8 @@ export {
useFormChild,
useMeta,
useQuasar,

useHydration,
useRenderCache,
useSplitAttrs,
useTick,
Expand Down
16 changes: 0 additions & 16 deletions ui/src/composables/private/use-can-render.js

This file was deleted.

16 changes: 16 additions & 0 deletions ui/src/composables/use-hydration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ref, onMounted } from 'vue'

// using it to manage SSR rendering with best performance
import { isRuntimeSsrPreHydration } from '../plugins/Platform.js'

export default function () {
const isHydrated = ref(!isRuntimeSsrPreHydration.value)

if (isHydrated.value === false) {
onMounted(() => {
isHydrated.value = true
})
}

return { isHydrated }
}
10 changes: 7 additions & 3 deletions ui/types/composables.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ interface UseFormChildOptions {

export function useFormChild(options: UseFormChildOptions): void;

export function useHydration(): {
isHydrated: Ref<boolean>;
};

export function useMeta(options: MetaOptions | (() => MetaOptions)): void;

export function useQuasar(): QVueGlobals;

export function useRenderCache(): {
getCache: <T = any>(key: string, defaultValue?: T | (() => T)) => T | undefined;
getCache: <T = any>(key: string, defaultValue?: T | (() => T)) => T;
setCache: <T = any>(key: string, value: T) => void;
hasCache: (key: string) => boolean;
clearCache: (key?: string) => void;
};

export function useSplitAttrs(): {
attributes: Ref<Record<string, any>>;
listeners: Ref<Record<string, any>>;
attributes: Ref<Record<string, string | null | undefined>>;
listeners: Ref<Record<string, (...args: any[]) => any>>;
};

export function useTick(): {
Expand Down

0 comments on commit 05ec5b6

Please sign in to comment.