Skip to content

fix: Prevent StyledWidgets from being focusable by default #551

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tilucasoli
Copy link
Collaborator

@tilucasoli tilucasoli commented Jan 21, 2025

Description

By default, all StyledWidgets are focusable, even in cases where they shouldn’t be. In vanilla Flutter, a Container is not focusable unless explicitly wrapped with another widget to enable focus. This PR aims to replicate that behavior in StyledWidgets, making them focusable only when a FocusableVariant is explicitly defined in their style. This change aligns StyledWidgets with expected Flutter behavior, providing better control over focusable elements.

Changes

  • Fix canRequestFocus property on Interactive;
  • Add a logic in StyledSpec to only set canRequestFocus when needed
  • Add some tests

Review Checklist

  • Testing: Have you tested your changes, including unit tests and integration tests for affected code?
  • Breaking Changes: Does this change introduce breaking changes affecting existing code or users?
  • Documentation Updates: Are all relevant documentation files (e.g. README, API docs) updated to reflect the changes in this PR?
  • Website Updates: Is the website containing the updates you make on documentation?

Summary by CodeRabbit

  • New Features

    • Enhanced focus management for interactive elements by allowing them to conditionally request focus based on styling. This results in more predictable and responsive behavior.
  • Tests

    • Expanded test coverage confirms that interactive elements correctly handle focus across various configurations, improving overall reliability.

@tilucasoli tilucasoli requested a review from leoafarias January 21, 2025 16:09
Copy link

vercel bot commented Jan 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
mix-docs ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2025 9:06pm

@github-actions github-actions bot added the mix label Jan 21, 2025
@tilucasoli tilucasoli changed the title fix: canRequestFocus property on InteractiveMixStateWidget fix: Prevent StyledWidgets from being focusable by default Jan 23, 2025
@tilucasoli
Copy link
Collaborator Author

@coderabbitai

Copy link

coderabbitai bot commented Mar 19, 2025

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get PR #551 details
gh pr view 551 --json files,commits

# List the files changed in PR #551
gh pr view 551 --json files --jq '.files[].path'

Length of output: 1459


🏁 Script executed:

#!/bin/bash
# Examine the changed files in detail
echo "===== styled_widget.dart ====="
git show 88ffb71444fe525d25b782b9336bf69daa1a515e -- packages/mix/lib/src/core/styled_widget.dart

echo -e "\n\n===== interactive_mix_state.dart ====="
git show 88ffb71444fe525d25b782b9336bf69daa1a515e -- packages/mix/lib/src/core/widget_state/internal/interactive_mix_state.dart

# Look at the relevant test file changes to understand the testing approach
echo -e "\n\n===== styled_widget_test.dart (partial) ====="
git show 88ffb71444fe525d25b782b9336bf69daa1a515e -- packages/mix/test/src/core/styled_widget_test.dart | head -n 100

# Check if there's a FocusableVariant class defined
echo -e "\n\n===== Checking for FocusableVariant definition ====="
rg -A 5 "class FocusableVariant" packages/mix/

Length of output: 13689


🏁 Script executed:

#!/bin/bash
# Search for OnFocusedVariant definition
echo "===== Searching for OnFocusedVariant definition ====="
rg -A 5 "class OnFocusedVariant" packages/mix/

# Get the full styled_widget_test.dart to see the test cases
echo -e "\n\n===== Full styled_widget_test.dart ====="
cat packages/mix/test/src/core/styled_widget_test.dart

# Get the full pressable_widget_test.dart to see the new tests
echo -e "\n\n===== New pressable_widget_test.dart ====="
cat packages/mix/test/src/widgets/pressable/pressable_widget_test.dart

# Get the implementation of the Interactable class
echo -e "\n\n===== Looking for Interactable class implementation ====="
rg -A 10 "class Interactable" packages/mix/

Length of output: 32874


@tilucasoli Based on my analysis of the PR, I can see you've implemented a fix to prevent StyledWidgets from being focusable by default, aligning with vanilla Flutter behavior where a Container isn't focusable unless explicitly wrapped.

The key changes include:

  1. Adding conditional logic in SpecBuilder to only set canRequestFocus: true when a $on.focus variant exists in the style
  2. Using ExcludeFocus in InteractiveMixStateWidget to properly handle focus exclusion
  3. Adding comprehensive tests to verify this behavior works correctly

These changes make the behavior more intuitive and match users' expectations from regular Flutter widgets. The implementation looks solid and well-tested.

Would you like me to review anything specific in the PR?

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

coderabbitai bot commented Mar 19, 2025

Walkthrough

The changes update the widget focus behavior. A new boolean flag, canRequestFocus, is introduced in the SpecBuilder to determine focusability based on style variants. This flag is then passed to the Interactable widget. Additionally, the interactive state now wraps the FocusableActionDetector with an ExcludeFocus widget, which conditionally excludes focus based on canRequestFocus. New test cases have been added and restructured to verify these focus behaviors for both SpecBuilder and Pressable/Interactable components.

Changes

Files Change Summary
packages/mix/lib/src/core/styled_widget.dart
packages/mix/lib/src/core/widget_state/internal/interactive_mix_state.dart
Updated widget focus handling: Added canRequestFocus in SpecBuilder based on style variants and passed it to the Interactable widget; modified interactive state build method to wrap FocusableActionDetector within ExcludeFocus using !canRequestFocus.
packages/mix/test/src/core/styled_widget_test.dart
packages/mix/test/src/widgets/pressable/pressable_widget_test.dart
Added and restructured tests for verifying widget focus behavior; introduced the _testFocusability helper function to streamline focus testing for Pressable/Interactable widgets under different canRequestFocus scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant SB as SpecBuilder
    participant I as Interactable
    participant IS as _InteractiveStateBuilder
    participant EX as ExcludeFocus
    participant FAD as FocusableActionDetector

    SB->>SB: Check style variants for focus
    SB->>I: Pass calculated canRequestFocus flag
    I->>IS: Build widget state with focus setting
    IS->>EX: Wrap FocusableActionDetector (excluding = !canRequestFocus)
    EX->>FAD: Initialize focus actions
Loading

Poem

Oh, in code fields I hop along,
With focus bright, I sing my song.
My whiskers twitch at each new flag,
Ensuring every widget does not lag.
In the land of mix, I proudly bound,
A joyful code bunny, happiness found!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 10083ca and 88ffb71.

📒 Files selected for processing (4)
  • packages/mix/lib/src/core/styled_widget.dart (1 hunks)
  • packages/mix/lib/src/core/widget_state/internal/interactive_mix_state.dart (1 hunks)
  • packages/mix/test/src/core/styled_widget_test.dart (2 hunks)
  • packages/mix/test/src/widgets/pressable/pressable_widget_test.dart (3 hunks)
🔇 Additional comments (8)
packages/mix/lib/src/core/styled_widget.dart (1)

143-149: Effective implementation of conditional focus behavior.

The implementation correctly determines if a widget can request focus based on the presence of OnFocusedVariant in the style, and passes this information to the Interactable widget. This approach aligns with Flutter's standard behavior where widgets like Container are not focusable by default.

packages/mix/lib/src/core/widget_state/internal/interactive_mix_state.dart (1)

91-106: Good use of ExcludeFocus to conditionally prevent focus.

Wrapping the FocusableActionDetector with an ExcludeFocus widget is an elegant solution. The excluding: !widget.canRequestFocus approach correctly implements the requirement to prevent focus by default while preserving all other functionality of the original widget.

packages/mix/test/src/widgets/pressable/pressable_widget_test.dart (3)

66-90: Comprehensive test coverage for Pressable focus behavior.

The tests effectively verify that the Pressable widget respects the canRequestFocus property in both true and false states.


669-693: Good test coverage for Interactable focus behavior.

Similar to the Pressable tests, these tests ensure that the Interactable widget correctly handles the canRequestFocus property, verifying both when focus should be allowed and when it should be prevented.


788-806: Well-designed test helper function.

The _testFocusability function is a good example of code reuse that reduces duplication and improves test clarity. It encapsulates the common test logic for verifying focus behavior.

packages/mix/test/src/core/styled_widget_test.dart (3)

89-225: Comprehensive test coverage for SpecBuilder behaviors.

These tests provide thorough verification of various SpecBuilder behaviors including controller interactions, widget wrapping based on style variants, and proper rendering with different modifier types.


241-270: Excellent tests for the new focus behavior.

These tests specifically verify that the canRequestFocus property is correctly set based on the presence or absence of $on.focus in the Style. This ensures the core requirement of the PR is properly implemented and tested.


274-294: Well-designed test helper function.

The _testFocusability helper function effectively encapsulates the common logic for testing focus behavior, reducing code duplication and improving test maintainability.


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@github-actions github-actions bot added documentation Improvements or additions to documentation remix repo labels Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation mix remix repo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant