Skip to content

fix: pass data attributes to withUniwind#477

Merged
Brentlok merged 3 commits intomainfrom
fix/withUniwind-data-attributes
Mar 26, 2026
Merged

fix: pass data attributes to withUniwind#477
Brentlok merged 3 commits intomainfrom
fix/withUniwind-data-attributes

Conversation

@Brentlok
Copy link
Contributor

@Brentlok Brentlok commented Mar 26, 2026

fixes #475

Summary by CodeRabbit

  • Refactor

    • Styling system now accepts and propagates component props (including data-attributes) into style resolution and injects generated data props into wrapped components.
    • Data-attribute keys are normalized (kebab-case → camelCase) and accepted values may be undefined.
  • Tests

    • Added/updated tests verifying data-attribute handling and props integration with style resolution.
  • Chores

    • Upgraded dev tooling (turbo) to a newer patch version.

@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ed06dc11-d973-42b1-80a3-d7366f5073bc

📥 Commits

Reviewing files that changed from the base of the PR and between b8b28d8 and 1f78e28.

📒 Files selected for processing (1)
  • packages/uniwind/src/components/web/generateDataSet.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/uniwind/src/components/web/generateDataSet.ts

📝 Walkthrough

Walkthrough

getWebStyles gained a componentProps parameter used to populate a temporary off‑DOM dataset; HOCs and hooks pass component props and inject generateDataSet output into wrapped components; dataset typing widened; tests updated for data-attribute handling; root devDependency turbo bumped to 2.8.20.

Changes

Cohort / File(s) Summary
Dependency Update
package.json
Bump devDependency turbo 2.8.17 → 2.8.20.
Core Function Signature
packages/uniwind/src/core/web/getWebStyles.ts
Add componentProps?: Record<string, unknown> parameter; create temporary off‑DOM dummy.dataset from props (filter/stringify values), compute active CSS rules, then remove temporary dataset entries.
HOCs (web & native)
packages/uniwind/src/hoc/withUniwind.tsx, packages/uniwind/src/hoc/withUniwind.native.tsx
Thread component props into getWebStyles calls; web HOC injects dataSet={generateDataSet(props)} into wrapped components.
Hooks & consumers
packages/uniwind/src/hooks/useResolveClassNames.ts, packages/uniwind/tests/e2e/getWebStyles.test.ts
Update callers to include the new middle argument (often undefined in some paths) to match the new getWebStyles signature.
DataSet generation & types
packages/uniwind/src/components/web/generateDataSet.ts
Convert data-* suffixes from kebab-case to camelCase in returned dataSet; widen DataSet type to allow undefined, and update RN module augmentation props to dataSet?: DataSet.
Tests
packages/uniwind/tests/native/hoc/withUniwind.test.tsx, packages/uniwind/tests/web/hoc/withUniwind.test.tsx
Add native test ensuring non-style data props don't interfere; update web test expectations to include component props object as getWebStyles second arg.

Sequence Diagram(s)

sequenceDiagram
    participant Comp as Component
    participant HOC as withUniwind HOC
    participant Gen as generateDataSet
    participant Styles as getWebStyles
    participant DOM as Off‑DOM Dummy Element

    Comp->>HOC: render(props, className)
    HOC->>Gen: generateDataSet(props)
    Gen-->>HOC: dataSet
    HOC->>Styles: getWebStyles(className, props, context)
    Styles->>DOM: set dataset entries (from props)
    Styles->>DOM: compute active CSS rules
    DOM-->>Styles: matched rules / computed styles
    Styles->>DOM: remove temporary dataset entries
    Styles-->>HOC: resolved RN styles
    HOC-->>Comp: pass generatedProps (including dataSet)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐇
I nibble props and stitch their names,
data-keys dancing, calling rules by name,
HOC hums softly, DOM plays the game,
Styles return — a rabbit’s stitched frame.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: passing data attributes to the withUniwind HOC, which is the core objective addressed in the PR.
Linked Issues check ✅ Passed The PR successfully implements data-attribute support for withUniwind by modifying getWebStyles to accept componentProps, updating HOC calls to pass props, and fixing generateDataSet to handle kebab-to-camelCase conversion.
Out of Scope Changes check ✅ Passed All changes are in-scope: turbo version bump supports the build process, and all functional changes directly support data-attribute passing through withUniwind.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/withUniwind-data-attributes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
packages/uniwind/src/core/web/getWebStyles.ts (1)

55-59: componentProps parameter is accepted but unused.

The native implementation in store.ts validates data-attribute requirements against componentProps via validateDataAttributes(style.dataAttributes, componentProps), skipping styles when requirements aren't met.

The web implementation accepts the same parameter for API consistency but doesn't use it. On web, this likely works because the dataSet prop is forwarded to the DOM element, allowing browser CSS selectors (e.g., [data-test]) to match naturally.

Consider adding a brief comment explaining why componentProps is intentionally unused on web to prevent future confusion or accidental removal.

📝 Proposed documentation
 export const getWebStyles = (
     className: string | undefined,
+    // componentProps is accepted for API consistency with native but unused on web.
+    // Web relies on browser CSS selector matching via the dataSet prop instead of runtime validation.
     componentProps: Record<string, unknown> | undefined,
     uniwindContext: UniwindContextType,
 ): RNStyle => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/uniwind/src/core/web/getWebStyles.ts` around lines 55 - 59, The
parameter componentProps on getWebStyles is accepted but intentionally unused;
add a concise inline comment inside getWebStyles explaining that componentProps
is preserved for API parity with the native implementation (see
validateDataAttributes in store.ts) and that on web data-attributes are handled
by forwarding dataSet to the DOM and matched by CSS selectors, so no runtime
validation is required here—this prevents future maintainers from removing the
parameter.
packages/uniwind/tests/web/hoc/withUniwind.test.tsx (1)

35-54: Consider adding a test for dataSet prop injection.

The HOC now injects dataSet={generateDataSet(props)}, but there's no test verifying this behavior on web. Consider adding a test that confirms data-* props are converted to dataSet and passed to the wrapped component.

💡 Example test case
test('[auto] Should convert data-* props to dataSet', () => {
    ComponentWithSpy.mockClear()
    const AutoWithUniwind = withUniwind(ComponentWithSpy)

    render(<AutoWithUniwind data-active={true} data-state="open" testID="test-component" />)

    const receivedProps = ComponentWithSpy.mock.calls[0][0]
    expect(receivedProps.dataSet).toEqual({
        active: true,
        state: 'open',
    })
})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/uniwind/tests/web/hoc/withUniwind.test.tsx` around lines 35 - 54,
Add a test that verifies withUniwind injects a dataSet prop produced by
generateDataSet: clear ComponentWithSpy mock, create AutoWithUniwind =
withUniwind(ComponentWithSpy), render AutoWithUniwind with data-active and
data-state (and testID), then inspect ComponentWithSpy.mock.calls[0][0] and
assert receivedProps.dataSet equals { active: true, state: 'open' }; use the
same render/test utilities and access pattern as the existing tests so it
mirrors the color mapping test.
packages/uniwind/tests/e2e/getWebStyles.test.ts (1)

66-84: Consider adding e2e tests for data-attribute selectors.

Given that the PR objective is to fix data-* attribute selectors for withUniwind, it would be valuable to add e2e tests verifying that classes like data-active:bg-red-500 resolve correctly when the corresponding data attribute is present on the element.

Would you like me to draft test cases for data-attribute selector scenarios?

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/uniwind/tests/e2e/getWebStyles.test.ts` around lines 66 - 84, Add
e2e tests in the same suite (packages/uniwind/tests/e2e/getWebStyles.test.ts)
that mirror the existing pattern but cover data-attribute selectors: call
getWebStyles(page, 'data-active:bg-red-500') against an element that has the
corresponding data-active attribute (use withUniwind behavior/setup) and assert
the returned styles include backgroundColor: TW_RED_500; also add a negative
test where the element lacks the data-active attribute and expect an empty
object. Reference getWebStyles and the data-attribute variant
'data-active:bg-red-500' when adding these cases so they run alongside the
existing bg-red-500/text-base tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/uniwind/src/core/web/getWebStyles.ts`:
- Around line 55-59: The parameter componentProps on getWebStyles is accepted
but intentionally unused; add a concise inline comment inside getWebStyles
explaining that componentProps is preserved for API parity with the native
implementation (see validateDataAttributes in store.ts) and that on web
data-attributes are handled by forwarding dataSet to the DOM and matched by CSS
selectors, so no runtime validation is required here—this prevents future
maintainers from removing the parameter.

In `@packages/uniwind/tests/e2e/getWebStyles.test.ts`:
- Around line 66-84: Add e2e tests in the same suite
(packages/uniwind/tests/e2e/getWebStyles.test.ts) that mirror the existing
pattern but cover data-attribute selectors: call getWebStyles(page,
'data-active:bg-red-500') against an element that has the corresponding
data-active attribute (use withUniwind behavior/setup) and assert the returned
styles include backgroundColor: TW_RED_500; also add a negative test where the
element lacks the data-active attribute and expect an empty object. Reference
getWebStyles and the data-attribute variant 'data-active:bg-red-500' when adding
these cases so they run alongside the existing bg-red-500/text-base tests.

In `@packages/uniwind/tests/web/hoc/withUniwind.test.tsx`:
- Around line 35-54: Add a test that verifies withUniwind injects a dataSet prop
produced by generateDataSet: clear ComponentWithSpy mock, create AutoWithUniwind
= withUniwind(ComponentWithSpy), render AutoWithUniwind with data-active and
data-state (and testID), then inspect ComponentWithSpy.mock.calls[0][0] and
assert receivedProps.dataSet equals { active: true, state: 'open' }; use the
same render/test utilities and access pattern as the existing tests so it
mirrors the color mapping test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 34242f46-794b-4342-9af0-106a67bd0007

📥 Commits

Reviewing files that changed from the base of the PR and between 54b4852 and f7c791a.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • package.json
  • packages/uniwind/src/core/web/getWebStyles.ts
  • packages/uniwind/src/hoc/withUniwind.native.tsx
  • packages/uniwind/src/hoc/withUniwind.tsx
  • packages/uniwind/src/hooks/useResolveClassNames.ts
  • packages/uniwind/tests/e2e/getWebStyles.test.ts
  • packages/uniwind/tests/native/hoc/withUniwind.test.tsx
  • packages/uniwind/tests/web/hoc/withUniwind.test.tsx

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/uniwind/src/core/web/getWebStyles.ts`:
- Around line 77-91: The current loop that writes attributes using dummy.dataset
(from generateDataSet(componentProps)) fails for hyphenated keys and omits
aria-* attributes, and cleanup can be skipped if
getActiveStylesForClass(className) throws; replace the dataset assignment with
element.setAttribute(key, String(value)) and ensure you also include any aria-*
entries from generateDataSet, and wrap the call to
getActiveStylesForClass(className) in a try/finally so that in finally you
remove the same attributes via dummy.removeAttribute(key) (use the same keys you
set) to guarantee cleanup even on exceptions.
🪄 Autofix (Beta)

❌ Autofix failed (check again to retry)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eb60fca8-1006-46e0-b979-a23a8938b9e6

📥 Commits

Reviewing files that changed from the base of the PR and between f7c791a and b8b28d8.

📒 Files selected for processing (2)
  • packages/uniwind/src/components/web/generateDataSet.ts
  • packages/uniwind/src/core/web/getWebStyles.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/uniwind/src/components/web/generateDataSet.ts

@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2026

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

Autofix skipped. No unresolved CodeRabbit review comments with fix instructions found.

@Brentlok Brentlok merged commit 01a6b5e into main Mar 26, 2026
2 checks passed
@Brentlok Brentlok deleted the fix/withUniwind-data-attributes branch March 26, 2026 08:11
@github-actions
Copy link
Contributor

🚀 This pull request is included in v1.6.1. See Release v1.6.1 for release notes.

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.

data-* attributes don't work with withUniwind

1 participant