npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jangxyz/svelte-id-refs

v0.1.0

Published

Manage page-level id uniqueness in Svelte components

Downloads

4

Readme

Id Refs Svelte

Make id attributes reusable in components, for Svelte.

<script>
  const idRefs = createIdRefsContext()
</script>

<WithId idKey="name" {idRefs} let:id>
  <label for={id}>Name:</label>
  <input {id} type="text" bind:value={name} />
</WithId>

Why?

Id attributes should be unique in a DOM tree. That's why we have getElementById() method, opposed to getElementsByClass().

However, as component-based methodologies prevail, components started to focus on reusability, which conflicts with the idea of "allowing an attribute to be used only once throughout the entire page". This can be a problem when we are building components, because we do not know in advance whether it will be used only once or multiple times, perhaps even in the future.

We cannot ditch using id all together, because that's not how the web works. Rather, the number of attributes that depend on the attribute keeps on growing, starting from the for attribute in <label> tags and others like aria-labelledby. Deciding not to use id is like deciding not to use some aspects of what the web as a platform is providing.

This package resolves this problem by appending random suffices to the id key, while maintaining the key suffix you want to highlight. It aims to provide an user-friendly interface as a declarative syntax.

Install

npm install @jangxyz/id-refs-svelte

Usage

Usage 1 - Using createIdRefsContext

idRefs maintains unique id values.

<script>
  import { createIdRefsContext } from '$lib/idRefs.js'

  let name = ''

  const idRefs = createIdRefsContext({ suffix: 3 })
  const nameId = idRefs.newId('name')
  const emailId = idRefs.newId('email')
</script>

<label for={nameId}>Name:</label>
<input id={nameId} type="text" bind:value={name} />

<label for={emailId}>E-mail:</label>
<input id={emailId} type="email" bind:value={email} />

Usage 2 - Using the context in child components

createIdRefsContext registers a context that can be used by descendant components.

<!-- +page.svelte -->
<script>
  import { createIdRefsContext } from '$lib/idRefs.js'
  import Card from './Card.svelte'

  createIdRefsContext({ suffix: 3 })
</script>

<Card name="user1" />
<Card name="user2" />

Inside each component, you call the idRef context with useIdRefs(), and use the helper methods to ensure unique id names.

There is a helper component <WithId> that wraps the idRefs.newId() invocation inside a component. This way you can put the id declaration part close to its usage, making the code more readable.

<!-- Card.svelte -->
<script>
  import { useIdRefs } from '$lib/idRefs.js'
  import WithId from '$lib/WithId.svelte'

  const idRefs = useIdRefs()

  export let name = ''
</script>

<fieldset>
  <legend>Card</legend>

  <div class="grid">
  	<WithId idKey="name" {idRefs} let:id={nameId}>
  		<label for={nameId} title="id={nameId}">Name</label>
  		<input id={nameId} name="name" value={name} />
  	</WithId>

  	<WithId idKey="email" let:id={emailId}>
  		<label for={emailId} title="id={emailId}">Email</label>
  		<input id={emailId} name="email" />
  	</WithId>
  </div>
</fieldset>

For convenience, idRefs prop in WithId can be omitted, which will call useIdRefs() internally.