Skip to content

fix: keep border style changes made while focused - #163

Merged
grindlemire merged 2 commits into
mainfrom
fix/focus-border-style
Sep 16, 2026
Merged

grindlemire merged 2 commits into
mainfrom
fix/focus-border-style

Conversation

@grindlemire

@grindlemire grindlemire commented Sep 16, 2026 •

Copy link
Copy Markdown
Owner

Changing an element's border color while it was focused was lost on blur. Focus() stashed the border style and swapped in the cyan highlight, but SetBorderStyle, WithBorderStyle applied through Apply, and SetClass with a border color all wrote straight to borderStyle. That replaced the highlight for the rest of the focus period, and Blur() then put back the color captured at focus time, discarding the change. SetClass also snapshotted the highlight as its pre-class value, so clearing the class while focused baked cyan into the base style.

Every border style writer now goes through one slot on Element that points at the saved unfocused style while a highlight is showing and at borderStyle otherwise. The highlight stays visible, and Blur restores whatever was written last. The class snapshot and restore read the same slot. Input and TextArea apply their focus color through the same highlight helper so writes to their rendered root follow the rule. Two behavior changes ride along. A bordered element with an onBlur handler and no onFocus handler used to keep the cyan highlight forever after blur, because Blur skipped the restore whenever onBlur was set; it now restores the unfocused style like any other element, and it does so before calling onBlur so a handler that refocuses gets a fresh highlight. BorderStyle() now returns the unfocused style while the default highlight is showing, matching what SetBorderStyle set, instead of the cyan highlight.

e := tui.New(tui.WithBorder(tui.BorderSingle), tui.WithBorderStyle(red), tui.WithFocusable(true))
e.Focus()                 // cyan highlight
e.SetBorderStyle(green)   // highlight still visible
e.Blur()                  // border is green

Fixes #153

Route every border style write (SetBorderStyle, WithBorderStyle via Apply,
SetClass border colors) through a slot that targets the saved unfocused
style while a focus highlight occupies borderStyle, so the highlight stays
visible and Blur restores the latest style instead of the one captured at
focus time. The class snapshot reads the same slot so clearing a class
while focused does not bake the highlight into the base style. Input and
TextArea apply their focus color through the same highlight helper, and
Blur restores a saved style regardless of an onBlur handler.

Fixes #153
@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.62%. Comparing base (64e001a) to head (b61f324).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #163      +/-   ##
==========================================
+ Coverage   84.49%   84.62%   +0.12%     
==========================================
  Files         169      169              
  Lines       21535    21537       +2     
==========================================
+ Hits        18197    18226      +29     
+ Misses       2669     2646      -23     
+ Partials      669      665       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@greptile-apps

greptile-apps Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previously reported refocus problem is fixed and no new actionable defects remain.

Summary

This PR centralizes border-style writes through the element’s saved unfocused-style slot so changes made while a focus highlight is visible survive blur.

  • Preserves default, Input, and TextArea focus highlights while updating the underlying border style.
  • Restores border state before invoking onBlur, allowing callbacks to refocus safely.
  • Makes class capture and restoration operate on the unfocused border style.
  • Documents and tests the revised BorderStyle() contract and focus lifecycle behavior.

Reviews (2) · Last reviewed commit: "fix: restore border before onBlur and re..."

Comment thread element_focus.go Resolved
…orderStyle()

Blur() now restores the saved style and clears the stash before invoking
onBlur, so a handler that refocuses gets a fresh highlight instead of
losing it.

BorderStyle() reads the same slot SetBorderStyle writes, so it round-trips
while the default highlight is showing. The reference doc is updated to
say so.

The two linear SetClass tests are folded into a table with a new
apply-before-focus row, plus a refocus-in-onBlur test.
@grindlemire

Copy link
Copy Markdown
Owner Author

@greptileai please review again

@grindlemire
grindlemire merged commit bf66969 into main Sep 16, 2026
11 checks passed
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.

Border color changes while focused are lost on blur

1 participant