Skip to content

Client directive: Hover

The client:hover directive hydrates the component when clicking on it or when the specified element is clicked.

Setup

For setting up the directive, include the integration in your Astro project.

  1. Install the library using your preferred package manager:
    npm i -D @astro-tools/client-directives
  2. Add the integration to your project configuration:
    astro.config.mjs
    import { defineConfig } from 'astro/config';
    import { hoverClientDirective } from '@astro-tools/client-directives/hover';
    export default defineConfig({
    integrations: [hoverClientDirective()],
    });

With client:on directive

To integrate this directive with the client:on, configure the directive as follows:

astro.config.mjs
import { defineConfig } from 'astro/config';
import { onClientDirective } from '@astro-tools/client-directives/on';
export default defineConfig({
integrations: [
onClientDirective({
directives: [
{
name: 'hover',
entrypoint: '@astro-tools/client-directives/hover/directive',
},
],
}),
],
});

Use

Add the directive as attribute of the component to hydrate:

<MyComponent client:hover />

When hovering in any part of the component, the component will be hydrated and the event will be replayed using the target of the initial event.

---
import Square from '@/libs/examples/Square.svelte';
interface Props {
id: string;
}
const { id } = Astro.props;
---
<Square {id} text="Hover me!" client:hover />
Preview Static