Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update extended client:visible to use an object instead of a string #9596

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
'astro': minor
"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.
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved

```html
```astro
<!-- Load component when it's within 200px away from entering the viewport -->
<Component client:visible="200px" />
<Component client:visible={{rootMargin: "200px"}} />
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
```
8 changes: 5 additions & 3 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ 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'?: ClientVisibleOptions | boolean;
'client:only'?: boolean | string;
}

export type ClientVisibleOptions = Pick<IntersectionObserverInit, 'rootMargin'>;

export interface TransitionAnimation {
name: string; // The name of the keyframe
delay?: number | string;
Expand Down
9 changes: 6 additions & 3 deletions packages/astro/src/runtime/client/visible.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientDirective } from '../../@types/astro.js';
import type { ClientDirective, ClientVisibleOptions } from '../../@types/astro.js';

/**
* Hydrate this component when one of it's children becomes visible
Expand All @@ -11,8 +11,11 @@ const visibleDirective: ClientDirective = (load, options, el) => {
await hydrate();
};

const ioOptions = {
rootMargin: typeof options.value === 'string' ? options.value : undefined,
const rawOptions =
typeof options.value === 'object' ? (options.value as ClientVisibleOptions) : undefined;

Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final nit: this technically adds support for all IntersectionObserverInit values, but our types are narrowed to only support Pick<IntersectionObserverInit, 'rootMargin'>. Is that desired?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, see the code below

const ioOptions: IntersectionObserverInit = {
rootMargin: rawOptions?.rootMargin,
};

const io = new IntersectionObserver((entries) => {
Expand Down
Loading