diff --git a/.changeset/stupid-peas-juggle.md b/.changeset/stupid-peas-juggle.md deleted file mode 100644 index 1e01c0996636..000000000000 --- a/.changeset/stupid-peas-juggle.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -'astro': minor ---- - -Extends the `client:visible` directive by adding an optional `rootMargin` property. This allows a component to be hydrated when it is close to the viewport instead of waiting for it to become visible. - -```html - - -``` diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 74cbb4694d9d..2da7cc141af6 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -65,16 +65,16 @@ export type { export type { RemotePattern } from '../assets/utils/remotePattern.js'; export type { SSRManifest } from '../core/app/types.js'; export type { - AstroCookies, - AstroCookieSetOptions, AstroCookieGetOptions, + AstroCookieSetOptions, + AstroCookies, } from '../core/cookies/index.js'; export interface AstroBuiltinProps { 'client:load'?: boolean; 'client:idle'?: boolean; 'client:media'?: string; - 'client:visible'?: string | boolean; + 'client:visible'?: boolean; 'client:only'?: boolean | string; } diff --git a/packages/astro/src/runtime/client/visible.ts b/packages/astro/src/runtime/client/visible.ts index 9e625ca23df9..de36b29098a5 100644 --- a/packages/astro/src/runtime/client/visible.ts +++ b/packages/astro/src/runtime/client/visible.ts @@ -5,16 +5,12 @@ import type { ClientDirective } from '../../@types/astro.js'; * We target the children because `astro-island` is set to `display: contents` * which doesn't work with IntersectionObserver */ -const visibleDirective: ClientDirective = (load, options, el) => { +const visibleDirective: ClientDirective = (load, _options, el) => { const cb = async () => { const hydrate = await load(); await hydrate(); }; - const ioOptions = { - rootMargin: typeof options.value === 'string' ? options.value : undefined, - }; - const io = new IntersectionObserver((entries) => { for (const entry of entries) { if (!entry.isIntersecting) continue; @@ -23,7 +19,7 @@ const visibleDirective: ClientDirective = (load, options, el) => { cb(); break; // break loop on first match } - }, ioOptions); + }); for (const child of el.children) { io.observe(child);