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 1 commit
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
11 changes: 10 additions & 1 deletion packages/astro/e2e/custom-client-directives.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@playwright/test';
import { testFactory, waitForHydrate } from './test-utils.js';
import testAdapter from '../test/test-adapter.js';
import { testFactory, waitForHydrate } from './test-utils.js';

const test = testFactory({
root: './fixtures/custom-client-directives/',
Expand Down Expand Up @@ -89,4 +89,13 @@ function testClientDirectivesShared() {
// Hydrated, this should be 1
await expect(counterValue).toHaveText('1');
});

test('Client directives should be passed options correctly', async ({ astro, page }) => {
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
await page.goto(astro.resolveUrl('/'));

const clientOptions = page.locator('#options');
await expect(clientOptions).toHaveText(
'Passed options are: {"message":"Hello! I was passed as an option"}'
);
});
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineConfig } from 'astro/config';
import react from "@astrojs/react";
import { defineConfig } from 'astro/config';
import { fileURLToPath } from 'node:url';

export default defineConfig({
integrations: [astroClientClickDirective(), astroClientPasswordDirective(), react()],
integrations: [astroClientClickDirective(), astroClientPasswordDirective(), astroHasOptionsDirective(), react()],
});

function astroClientClickDirective() {
Expand Down Expand Up @@ -33,3 +33,17 @@ function astroClientPasswordDirective() {
}
};
}

function astroHasOptionsDirective() {
return {
name: 'astro-options',
hooks: {
'astro:config:setup': (opts) => {
opts.addClientDirective({
name: 'options',
entrypoint: fileURLToPath(new URL('./client-options.js', import.meta.url))
});
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Hydrate on first click on the window
export default async (load, options) => {
const hydrate = await load()
document.write(`<div id="options">Passed options are: ${JSON.stringify(options.value)}</div>`)
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
await hydrate()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
declare module 'astro' {
interface AstroClientDirectives {
'client:click'?: boolean
'client:password'?: string
'client:password'?: string
'client:options'?: { message: string }
}
}

// Make d.ts a module to similate common packaging setups where the entry `index.d.ts` would augment the types
export {}
export { }
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import Counter from '../components/Counter.jsx';
<body>
<Counter id="client-click" client:click>client:click</Counter>
<Counter id="client-password" client:password="hunter2">client:password</Counter>
<Counter id="client-has-options" client:options={{message: "Hello! I was passed as an option"}}>client:options</Counter>
</body>
</html>
</html>
Loading