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: add ID Attribute to Toggle Root Component #823

Merged
merged 2 commits into from
Oct 28, 2024

Conversation

max-got
Copy link

@max-got max-got commented Oct 28, 2024

Changes

It's me again. While working on nesting Toggle components within Tooltips (as part of the Origin UI to Svelte port), I discovered that the Toggle.Root component wasn't exposing its internal ID to the DOM. This made it particularly difficult to use the child snippet as using the Toggle.RootComponent as a Tooltip.Trigger is not possible.
See Stackblitz Repro

so i added the ID attribute to the Toggle Root element props for proper DOM referencing. All tests are still passing, so that is something.

Current Behavior

  • Toggle.Root uses ID internally for ref binding
  • The ID is not exposed in the rendered element's attributes

Implemented Solution

Added ID propagation to the rendered element's attributes in the ToggleRootState class:

props = $derived.by(
  () => ({
    [ROOT_ATTR]: "",
++   id: this.#id.current,
    "data-disabled": getDataDisabled(this.#disabled.current),
    // ... other existing props
  }) as const
);

Known Issues & Future Considerations

Note: There is a potential future conflict when using Toggle within Tooltip (and likely other bits) due to competing data-state attributes. This is not addressed in this PR but should be considered. A potential solution would be to just name the data-state attributes corresponding to the component, like data-tooltip-state, data-trigger-state,... (?)

Copy link

changeset-bot bot commented Oct 28, 2024

🦋 Changeset detected

Latest commit: 7399d75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
bits-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@max-got max-got changed the title Add ID Attribute to Toggle Root Component feat: add ID Attribute to Toggle Root Component Oct 28, 2024
Copy link
Contributor

github-actions bot commented Oct 28, 2024

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
bits-ui ✅ Ready (View Log) Visit Preview 7399d75

@max-got
Copy link
Author

max-got commented Oct 28, 2024

edited the Stackblitz Repro, old content was saved if you want to take a look again.

@huntabyte
Copy link
Owner

Thank you!

If you open an issue thread, we can discuss the idea of unique data—* attributes. I like the simplicity of data-state, but the point you bring up makes a lot of sense, and we definitely don't want to limit composition.

I'd like to address this while we're still in a pre-release because, obviously, this would be a breaking change should it be added after we release v1.

@huntabyte huntabyte merged commit 932ba90 into huntabyte:next Oct 28, 2024
5 checks passed
@github-actions github-actions bot mentioned this pull request Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants