8000 feat: useToggle by Shinigami92 · Pull Request #55 · un-ts/unuse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: useToggle #55

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

Merged
merged 1 commit into from
Jun 30, 2025
Merged

feat: useToggle #55

merged 1 commit into from
Jun 30, 2025

Conversation

Shinigami92
Copy link
Member
@Shinigami92 Shinigami92 commented Jun 28, 2025

Important

Introduces useToggle utility for managing toggleable state with tests and documentation updates.

  • New Features:
    • Introduced useToggle utility in index.ts for managing toggleable state with customizable values.
    • Exported useToggle in package exports.
  • Tests:
    • Added tests for useToggle in index.spec.ts, index.angular.spec.ts, index.react.spec.ts, index.solid.spec.ts, and index.vue.spec.ts.
  • Chores:
    • Added changeset fluffy-nights-try.md for documenting the new feature and minor version updates.

This description was created by Ellipsis for 85f9319. You can customize this summary. It will automatically update as commits are pushed.


Summary by CodeRabbit

  • New Features

    • Introduced a new useToggle utility for managing toggleable state with customizable values.
    • Made useToggle publicly available in the package exports.
  • Tests

    • Added comprehensive test suites to verify useToggle functionality, including support for custom values and integration with reactive references across multiple frameworks.
  • Chores

    • Added a changeset to document the new feature and upcoming minor version updates for related packages.

@Shinigami92 Shinigami92 self-assigned this Jun 28, 2025
@Shinigami92 Shinigami92 added the feature New feature label Jun 28, 2025
Copy link
changeset-bot bot commented Jun 28, 2025

🦋 Changeset detected

Latest commit: 8f13f7b

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

This PR includes changesets to release 5 packages
Name Type
unuse Minor
unuse-angular Minor
unuse-react Minor
unuse-solid Minor
unuse-vue Minor

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

Copy link
coderabbitai bot commented Jun 28, 2025

Warning

Rate limit exceeded

@Shinigami92 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 18 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 85f9319 and 8f13f7b.

📒 Files selected for processing (8)
  • .changeset/fluffy-nights-try.md (1 hunks)
  • packages/unuse/src/index.ts (1 hunks)
  • packages/unuse/src/useToggle/index.angular.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.react.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.solid.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.ts (1 hunks)
  • packages/unuse/src/useToggle/index.vue.spec.ts (1 hunks)

Walkthrough

A new useToggle utility has been introduced, providing a customizable toggleable state with support for custom truthy and falsy values. The utility is exported from the main entry point, and comprehensive test suites covering generic, Vue, Angular, React, and Solid.js environments have been added. A changeset documents minor version bumps for related packages.

Changes

File(s) Change Summary
.changeset/fluffy-nights-try.md Added a changeset declaring minor version updates and noting the new useToggle feature.
packages/unuse/src/index.ts Exported the new useToggle module from the package entry point.
packages/unuse/src/useToggle/index.ts Added the useToggle utility with customizable toggle logic and type-safe API.
packages/unuse/src/useToggle/index.spec.ts,
packages/unuse/src/useToggle/index.vue.spec.ts,
packages/unuse/src/useToggle/index.angular.spec.ts,
packages/unuse/src/useToggle/index.react.spec.ts,
packages/unuse/src/useToggle/index.solid.spec.ts
Added comprehensive test suites for useToggle covering generic, Vue, Angular, React, and Solid.js usage, including custom values and reactive state handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant useToggle
    participant Signal/Ref

    User->>useToggle: Call useToggle(initialValue, options?)
    useToggle->>Signal/Ref: Create or use reactive state
    useToggle-->>User: Return [state, toggleFn] or toggleFn

    User->>toggleFn: Call toggleFn() or toggleFn(value)
    toggleFn->>Signal/Ref: Update state (toggle or set explicit value)
    Signal/Ref-->>User: Updated state
Loading

Possibly related PRs

  • fix: reactivity #10: Modified internal toUnSignal function to improve bidirectional reactive synchronization, which is used internally by useToggle.

Poem

In the garden of code, a toggle appears,
Switching from truthy to falsy without any fears.
With tests all around and exports anew,
The state flips and flops, as all toggles do!
🐇✨
"Hop on, dear dev, and give it a try—
Your toggling dreams are now soaring high!"

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor
github-actions bot commented Jun 28, 2025

Coverage Report

Status Category Percentage Covered / Total
🔴 Lines 80.3% (🎯 90%)
⬆️ +1.64%
1215 / 1513
🔴 Statements 80.3% (🎯 90%)
⬆️ +1.64%
1215 / 1513
🔴 Functions 75.49% (🎯 90%)
⬆️ +0.49%
77 / 102
🔴 Branches 83.72% (🎯 85%)
⬆️ +2.77%
319 / 381
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/unuse/src/index.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
1-15
packages/unuse/src/useToggle/index.ts 100% 100% 100% 100%
Unchanged Files
packages/unuse-angular/src/index.ts 94.2% 100% 83.33% 94.2% 143-146
packages/unuse-react/src/index.ts 69.86% 68.18% 100% 69.86% 49-55, 58-71, 139-143, 152
packages/unuse-reactivity/src/index.ts 0% 0% 0% 0% 1-5
packages/unuse-reactivity/src/unComputed/index.ts 87.5% 81.25% 100% 87.5% 25-26, 29-30, 35-36
packages/unuse-reactivity/src/unEffect/index.ts 95.65% 50% 100% 95.65% 26
packages/unuse-reactivity/src/unEffectScope/index.ts 90.9% 50% 100% 90.9% 24-25
packages/unuse-reactivity/src/unReactiveSystem/index.ts 74.83% 94.73% 53.33% 74.83% 31-48, 68-69, 80-81, 92-93, 96-97, 100-103, 154-155, 181-192
packages/unuse-reactivity/src/unSignal/index.ts 100% 100% 100% 100%
packages/unuse-reactivity/src/unWatch/index.ts 100% 100% 100% 100%
packages/unuse-solid/src/index.ts 83.09% 86.95% 100% 83.09% 52-59, 69, 141-145
packages/unuse-vue/src/index.ts 100% 100% 100% 100%
packages/unuse/src/_framework/index.ts 81.53% 62.5% 100% 81.53% 48-49, 67-68, 76-77, 85-86, 94-95, 102-103
packages/unuse/src/_testUtils/angular.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/react.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/solid.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/vue.ts 100% 100% 100% 100%
packages/unuse/src/isClient/index.ts 100% 0% 100% 100%
packages/unuse/src/isObject/index.ts 100% 100% 100% 100%
packages/unuse/src/isWorker/index.ts 66.66% 0% 100% 66.66% 5
packages/unuse/src/toArray/index.ts 100% 50% 100% 100%
packages/unuse/src/toUnSignal/index.ts 80.7% 83.33% 66.66% 80.7% 17-18, 31-32, 35-36, 43-44, 81-87, 98-99, 113-120, 130
packages/unuse/src/tryOnScopeDispose/index.ts 77.77% 78.57% 50% 77.77% 11-14, 23-24, 35-36, 40-41
packages/unuse/src/unAccess/index.ts 73% 80% 66.66% 73% 43-44, 48-49, 66-67, 71, 80-81, 84-93, 97-98, 104-105, 114-115, 118-119, 122-123
packages/unuse/src/unRefElement/index.ts 75% 33.33% 100% 75% 29, 36-37
packages/unuse/src/unResolve/index.ts 92.66% 97.14% 71.42% 92.66% 71-72, 93-94, 125-128
packages/unuse/src/useEventListener/index.ts 96.34% 86.66% 100% 96.34% 192-193, 198
packages/unuse/src/useIntervalFn/index.ts 92.85% 80% 100% 92.85% 77-78, 82-83
packages/unuse/src/useWebSocket/index.ts 46.66% 41.66% 33.33% 46.66% 161-163, 203-212, 216-218, 222-224, 233-235, 244-256, 262-263, 270-275, 278-308, 311-312, 315-326, 330-355, 367-368
Generated in workflow #138 for commit 8f13f7b by the Vitest Coverage Report Action

@Shinigami92 Shinigami92 marked this pull request as ready for review June 28, 2025 20:47
Copy link
Contributor
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 8163ec1 in 1 minute and 11 seconds. Click for details.
  • Reviewed 318 lines of code in 5 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/unuse/src/useToggle/index.ts:63
  • Draft comment:
    Clarify behavior when toggle is called with an explicit undefined. The use of arguments.length to differentiate may be ambiguous if undefined is intentionally passed.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% The code's behavior is actually clear - it treats any passed value (including undefined) as a direct set operation, while no arguments triggers the toggle behavior. This is a common and valid pattern in JavaScript. The type signature (value?: T) makes it clear that undefined is a valid input. The behavior isn't ambiguous - it's intentionally designed this way. The comment raises a valid point about API design clarity. Some developers might expect undefined to always trigger the toggle behavior. While it's a thoughtful observation, the current implementation follows established patterns and is well-typed. The behavior is consistent and predictable once understood. The comment should be deleted as it suggests clarifying something that is already clear and well-implemented. The current code follows common JavaScript patterns.
2. packages/unuse/src/useToggle/index.ts:76
  • Draft comment:
    Document the dual return pattern (toggle function vs tuple) based on whether the initial value is a ref. This behavior could be surprising to some users.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
3. packages/unuse/src/useToggle/index.vue.spec.ts:10
  • Draft comment:
    Avoid excessive type assertions in tests. Consider refining the overload/types so that manual casts (as unknown as tuple) aren’t needed.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None

Workflow ID: wflow_yqLE5Yrt8PUDHC7X

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link
Contributor
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed 85f9319 in 1 minute and 19 seconds. Click for details.
  • Reviewed 680 lines of code in 8 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/fluffy-nights-try.md:2
  • Draft comment:
    Consider removing unnecessary quotes from keys in the changeset; unquoted keys may offer a more standard YAML style.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
2. packages/unuse/src/useToggle/index.react.spec.ts:45
  • Draft comment:
    Several tests here are marked as 'todo'. Ensure these tests are implemented once the React toggling behavior is finalized.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the PR author to ensure that tests are implemented in the future, which is not specific enough to be useful. It doesn't provide a specific suggestion or point out a specific issue with the current code. It also doesn't ask for a specific test to be written now, which would be acceptable.
3. packages/unuse/src/useToggle/index.ts:76
  • Draft comment:
    The overload returning just the toggle function when the initial value is a reactive reference (isUnRef true) may be confusing. Consider adding documentation to clarify this behavior.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 10% vs. threshold = 50% The behavior is already documented through TypeScript overloads which clearly show the two different return types. The function is also documented with JSDoc comments. The behavior follows a consistent pattern where UnRef values get simpler returns. Adding more documentation about this pattern would be redundant since it's a framework convention. The comment might have a point that users unfamiliar with the UnRef pattern could be confused by the different return types. The TypeScript types already provide clear documentation of this behavior, and it follows framework conventions. Additional documentation would be redundant. Delete the comment as the behavior is already well-documented through types and follows framework patterns.
4. packages/unuse/src/useToggle/index.react.spec.ts:39
  • Draft comment:
    For consistency with the default test, consider checking that the value is an array via Array.isArray instead of using toBeTypeOf('object').
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None

Workflow ID: wflow_gbbLD03FAvyOqvjf

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

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: 3

🧹 Nitpick comments (1)
packages/unuse/src/useToggle/index.ts (1)

53-53: Consider improving type safety for the default value.

The default value false as T could cause type issues if users provide custom truthy/falsy values that don't include false. While the type constraints should catch this at compile time, consider a more type-safe approach.

Consider this alternative approach:

-  initialValue: MaybeUnRef<T> = false as T,
+  initialValue: MaybeUnRef<T> = (false as any) as T,

Or better yet, make the default conditional:

-  initialValue: MaybeUnRef<T> = false as T,
+  initialValue?: MaybeUnRef<T>,

And handle the undefined case in the implementation by using falsyValue as the default.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8163ec1 and 85f9319.

📒 Files selected for processing (8)
  • .changeset/fluffy-nights-try.md (1 hunks)
  • packages/unuse/src/index.ts (1 hunks)
  • packages/unuse/src/useToggle/index.angular.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.react.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.solid.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.spec.ts (1 hunks)
  • packages/unuse/src/useToggle/index.ts (1 hunks)
  • packages/unuse/src/useToggle/index.vue.spec.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/unuse/src/index.ts
  • .changeset/fluffy-nights-try.md
🧰 Additional context used
🧬 Code Graph Analysis (5)
packages/unuse/src/useToggle/index.solid.spec.ts (2)
packages/unuse/src/_testUtils/solid.ts (1)
  • describeSolid (11-26)
packages/unuse/src/useToggle/index.ts (2)
  • useToggle (48-81)
  • ToggleFn (7-11)
packages/unuse/src/useToggle/index.react.spec.ts (2)
packages/unuse/src/_testUtils/react.ts (1)
  • describeReact (11-26)
packages/unuse/src/useToggle/index.ts (2)
  • useToggle (48-81)
  • ToggleFn (7-11)
packages/unuse/src/useToggle/index.ts (1)
packages/unuse/src/unAccess/index.ts (2)
  • MaybeUnRef (128-128)
  • unAccess (148-191)
packages/unuse/src/useToggle/index.spec.ts (2)
packages/unuse/src/useToggle/index.ts (1)
  • useToggle (48-81)
packages/unuse-reactivity/src/unSignal/index.ts (3)
  • isUnSignal (69-79)
  • UnSignal (17-34)
  • unSignal (45-64)
packages/unuse/src/useToggle/index.vue.spec.ts (2)
packages/unuse/src/_testUtils/vue.ts (1)
  • describeVue (15-30)
packages/unuse/src/useToggle/index.ts (2)
  • useToggle (48-81)
  • ToggleFn (7-11)
🪛 ESLint
packages/unuse/src/useToggle/index.solid.spec.ts

[error] 2-2: Unable to resolve path to module 'solid-js'.

(import-x/no-unresolved)

packages/unuse/src/useToggle/index.react.spec.ts

[error] 3-3: Unable to resolve path to module '@testing-library/react'.

(import-x/no-unresolved)


[error] 4-4: Unable to resolve path to module 'react'.

(import-x/no-unresolved)

packages/unuse/src/useToggle/index.angular.spec.ts

[error] 4-4: Unable to resolve path to module '@angular/core'.

(import-x/no-unresolved)


[error] 5-5: Unable to resolve path to module '@angular/core/testing'.

(import-x/no-unresolved)


[error] 9-9: Unable to resolve path to module '@angular/platform-browser/testing'.

(import-x/no-unresolved)

packages/unuse/src/useToggle/index.spec.ts

[error] 2-2: Unable to resolve path to module 'unuse-reactivity'.

(import-x/no-unresolved)

packages/unuse/src/useToggle/index.vue.spec.ts

[error] 3-3: Unable to resolve path to module 'vue'.

(import-x/no-unresolved)

🪛 GitHub Check: Lint: node-24, ubuntu-latest
packages/unuse/src/useToggle/index.react.spec.ts

[warning] 98-98:
The use of .todo is not recommended


[warning] 77-77:
The use of .todo is not recommended


[warning] 61-61:
The use of .todo is not recommended


[warning] 45-45:
The use of .todo is not recommended

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (9)
packages/unuse/src/useToggle/index.spec.ts (1)

1-98: Excellent comprehensive test coverage for the generic useToggle implementation.

This test suite thoroughly covers all the key functionality:

  • Default behavior and structure validation
  • Initial value handling (both explicit and default)
  • Toggle functionality (both parameter-less and with explicit values)
  • External signal integration
  • Custom truthy/falsy value configuration

The test assertions are well-structured and correctly validate both the return types and the reactive behavior of the hook.

packages/unuse/src/useToggle/index.solid.spec.ts (1)

1-96: Well-implemented Solid.js test suite with complete coverage.

This test suite properly validates the useToggle hook's integration with Solid.js:

  • Correctly uses createSignal for testing signal-based initial values
  • Proper type casting to Signal<boolean> and ToggleFn
  • Comprehensive coverage of all functionality including custom truthy/falsy values
  • No incomplete TODO tests, unlike the React version

The implementation demonstrates good understanding of Solid.js reactivity patterns.

packages/unuse/src/useToggle/index.vue.spec.ts (1)

1-96: Comprehensive Vue test suite with proper reactive integration.

This test suite effectively validates the useToggle hook's Vue integration:

  • Appropriate use of shallowRef for testing reactive references
  • Correct type casting to Ref<boolean> and validation with isRef
  • Complete test coverage including external ref integration and custom values
  • Consistent test patterns with other framework implementations

The Vue-specific reactive behavior is properly tested and validated.

packages/unuse/src/useToggle/index.angular.spec.ts (1)

1-126: Well-structured Angular test suite with proper signal integration.

This test suite correctly implements Angular-specific testing patterns:

  • Proper test environment initialization with BrowserTestingModule and ZonelessTestModule
  • Appropriate use of getTestBed().runInInjectionContext() for signal-based tests
  • Correct type casting to WritableSignal<boolean> and validation with isSignal
  • Comprehensive coverage of all scenarios including external signal integration

The Angular testing patterns are correctly applied and the suite provides complete coverage.

packages/unuse/src/useToggle/index.ts (5)

1-6: LGTM: Clean imports and proper dependencies.

The imports are well-organized and include all necessary utilities for the toggle implementation.


7-24: Excellent type definitions with comprehensive generics.

The type definitions provide great flexibility while maintaining type safety. The ToggleFn, UseToggleReturn, and UseToggleOptions types properly handle the generic constraints for custom truthy/falsy values.


26-41: Well-designed function overloads for different use cases.

The overloads correctly differentiate between:

  1. When initialValue is a reactive reference (UnRef<T>) - returns only the toggle function
  2. When initialValue is a regular value - returns tuple with reactive value and toggle function

This provides excellent developer experience and type safety.


62-74: Excellent resolution of the arguments.length issue.

The toggle function correctly uses a function declaration instead of an arrow function, which resolves the previous issue with arguments.length mentioned in the past review comments. The logic is sound:

  • When arguments are provided, it sets the value directly
  • When no arguments, it toggles between truthy and falsy values

The use of _value.get() after set/update operations ensures the fresh value is returned.


76-80: Smart return logic based on input type.

The conditional return logic is well-designed:

  • If the initial value was a reactive reference, return only the toggle function (user already has reactive access)
  • Otherwise, return both the readonly reactive value and toggle function

This provides optimal API ergonomics for different use cases.

@Shinigami92 Shinigami92 requested a review from JounQin June 29, 2025 15:25
@Shinigami92 Shinigami92 added this pull request to the merge queue Jun 30, 2025
Merged via the queue into main with commit 79dd6cb Jun 30, 2025
13 checks passed
@Shinigami92 Shinigami92 deleted the feat-useToggle branch June 30, 2025 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0