feat(headless): implement useTokenSelector hook#41
Conversation
- Added `useTokenSelector` hook to `packages/headless/src/theme.tsx` - Exported `useTokenSelector` from `packages/headless/src/index.ts` - Fixes issue where any `useTokens()` consumer re-renders on any theme change - Verified typecheck, lint, and test scripts across the monorepo Co-authored-by: truongnat <87919564+truongnat@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Implemented
useTokenSelectorin the headless package to selectively subscribe to theme tokens, resolving task T-13 fromIMPROVEMENT_PLAN.md.PR created automatically by Jules for task 8426291485733469266 started by @truongnat
🎯 This PR implements a new
useTokenSelectorhook in the headless package that allows components to selectively subscribe to specific theme tokens, preventing unnecessary re-renders when unrelated theme properties change. The hook uses a selector pattern similar to Redux'suseSelectorto optimize performance in theme-dependent components.🔍 Detailed Analysis
Key Changes
useTokenSelector<T>(selector: (tokens: SemanticTokens) => T): Tfunction inpackages/headless/src/theme.tsxwith memoization usinguseMemopackages/headless/src/index.tsto make it available to consumersTechnical Implementation
sequenceDiagram participant Component participant useTokenSelector participant useTokens participant ThemeProvider Component->>useTokenSelector: selector function useTokenSelector->>useTokens: get all tokens useTokens->>ThemeProvider: retrieve SemanticTokens ThemeProvider-->>useTokens: return tokens useTokens-->>useTokenSelector: return tokens useTokenSelector->>useTokenSelector: useMemo(selector(tokens)) useTokenSelector-->>Component: return selected value Note over useTokenSelector: Only re-renders when selected tokens changeImpact
useTokenSelectorwill only re-render when their specific selected tokens change, not on every theme updateuseTokens()usage remains unchanged, making this a non-breaking addition to the APICreated with Palmier
Summary by cubic
Adds
useTokenSelectorto theheadlesstheme API so components can subscribe to specific token slices and avoid unnecessary re-renders.useTokenSelector<T>(selector)to derive values fromSemanticTokensand memoize the result; exported viapackages/headless/src/index.ts.useTokenswhen you only need a subset of tokens; no breaking changes.Written for commit c38d877. Summary will update on new commits.