Skip to content

refactor(toggle-group): use Svelte 5 createContext for type-safe context - #2445

Open
braden-w wants to merge 1 commit into
huntabyte:mainfrom
braden-w:braden-w/context-factory-refactor
Open

refactor(toggle-group): use Svelte 5 createContext for type-safe context#2445
braden-w wants to merge 1 commit into
huntabyte:mainfrom
braden-w:braden-w/context-factory-refactor

Conversation

@braden-w

@braden-w braden-w commented Dec 7, 2025

Copy link
Copy Markdown
Contributor

This replaces the manual setContext/getContext wrapper functions with Svelte 5's createContext factory in the toggle-group component.

The new pattern from the Svelte 5 docs provides automatic type safety without requiring string keys or explicit type annotations:

// Before (10 lines)
import { getContext, setContext } from "svelte";
export function setToggleGroupCtx(props: ToggleVariants) {
    setContext("toggleGroup", props);
}
export function getToggleGroupCtx() {
    return getContext<ToggleVariants>("toggleGroup");
}

// After (2 lines)
import { createContext } from "svelte";
export const [getToggleGroupCtx, setToggleGroupCtx] = createContext<ToggleVariants>();

The consumer (toggle-group-item.svelte) doesn't require any changes since the getToggleGroupCtx() API remains identical.

This is a pure refactor with no behavioral changes.

Replace manual setContext/getContext wrapper functions with Svelte 5's
createContext factory. This provides automatic type safety without
requiring string keys or explicit type annotations.
@changeset-bot

changeset-bot Bot commented Dec 7, 2025

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c3ec75e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@huntabyte huntabyte left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

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