|
| 1 | +# simple scope 🔎 |
| 2 | + |
| 3 | +Get a scoped ID for whatever file you're in. Resolved at build-time with zero client JS. |
| 4 | + |
| 5 | +```jsx |
| 6 | +import { scope } from 'simple:scope'; |
| 7 | + |
| 8 | +function Form() { |
| 9 | + return ( |
| 10 | + <form> |
| 11 | + <label htmlFor={scope('email')}>Email</label> |
| 12 | + <input id={scope('email')} name="email" /> |
| 13 | + </form> |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +/* |
| 18 | +Output: |
| 19 | +
|
| 20 | +<form> |
| 21 | + <label for="email-dj23i_ka">Email</label> |
| 22 | + <input id="email-dj23i_ka" name="email"> |
| 23 | +</form> |
| 24 | +*/ |
| 25 | +``` |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +Simple scope is a vite plugin compatible with any vite-based framework (Astro, Nuxt, SvelteKit, etc). First install the dependency from npm: |
| 30 | + |
| 31 | +```bash |
| 32 | +npm i vite-plugin-simple-scope |
| 33 | +``` |
| 34 | + |
| 35 | +Then, set up type inferencing for the `simple:scope` module with an `env.d.ts` file. You can create this file at the base of your project, or add to the provided `src/env.d.ts` file for frameworks like Astro: |
| 36 | + |
| 37 | +```ts |
| 38 | +// env.d.ts |
| 39 | +/// <reference types="vite-plugin-simple-scope/types" /> |
| 40 | +``` |
| 41 | + |
| 42 | +Finally, apply as a vite plugin in your framework of choice: |
| 43 | + |
| 44 | +```js |
| 45 | +import simpleScope from 'vite-plugin-simple-scope'; |
| 46 | + |
| 47 | +// apply `simpleScope()` to your vite plugin config |
| 48 | +``` |
| 49 | + |
| 50 | +- [Astro vite plugin configuration](https://docs.astro.build/en/recipes/add-yaml-support/) |
| 51 | +- [Nuxt vite plugin configuration](https://nuxt.com/docs/getting-started/configuration#external-configuration-files) |
| 52 | +- [SvelteKit vite plugin configuration](https://kit.svelte.dev/docs/project-structure#project-files-vite-config-js) |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +You can import the `scope()` utility from `simple:scope` in any JavaScript-based file. This function accepts an optional prefix string for naming different scoped identifiers. |
| 57 | + |
| 58 | +Since `scope()` uses the file path to generate IDs, multiple calls to `scope()` will append the same value: |
| 59 | + |
| 60 | +```js |
| 61 | +// example.js |
| 62 | + |
| 63 | +scope(); // JYZeLezU |
| 64 | +scope('first'); // first-JYZeLezU |
| 65 | +scope('second'); // second-JYZeLezU |
| 66 | +``` |
| 67 | + |
| 68 | +Simple scope will also generate the same ID when called server-side or client-side. This prevents hydration mismatches when using component frameworks like React or Vue, and is helpful when querying scoped element `id`s from the DOM. |
| 69 | + |
| 70 | +This example uses [Astro](https://astro.build) to add a scoped `id` to a `<canvas>` element, and queries that `id` from a client-side `<script>`: |
| 71 | + |
| 72 | +```astro |
| 73 | +--- |
| 74 | +// Server-side template |
| 75 | +import { scope } from 'simple:scope'; |
| 76 | +--- |
| 77 | +
|
| 78 | +<canvas id={scope('canvas')}></canvas> |
| 79 | +
|
| 80 | +<script> |
| 81 | +// Client-side script |
| 82 | +import { scope } from 'simple:scope'; |
| 83 | +
|
| 84 | +const canvas = document.getElementById(scope('canvas')); |
| 85 | +</script> |
| 86 | +``` |
0 commit comments