Skip to content
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

fix(styles): Hide focus state when unnecessary and make design more coherent #2810

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/styles/src/mixins/_utilities.scss
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
}

@mixin focus-style($border-radius: commons.$border-radius, $vendor-prefix: '') {
&:is(:focus-visible, :focus-within, .pretend-focus)#{$vendor-prefix} {
// :has(:focus-visible) mimic a focus-visible-within pseudo-class
&:is(:focus-visible, :has(:focus-visible), .pretend-focus)#{$vendor-prefix} {
Copy link
Member

Choose a reason for hiding this comment

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

What's up with that vendor prefix stuff? It's not used anywhere and is bound to break the selector. Can we remove it?

Also, I would not want to change the border radius on focus if there was none before. I think those params can go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the vendor prefix, it is used here: https://github.com/swisspost/design-system/pull/2781/files#diff-f8aaec5942d0b7bdec7264460de3a7becec65dfa3ca88797a0875320dade340fR84
Previously it was used on more lines. Sure it look a bit odd, but it looks practical right now.

The border-radius could have unwanted side effects, you are right, but we need it to shape the outline. Perhaps, we want to add the outline into a ::after instead?

outline-offset: spacing.$size-line;
imagoiq marked this conversation as resolved.
Show resolved Hide resolved
outline: spacing.$size-line solid var(--post-focus-color);
border-radius: $border-radius;
Expand All @@ -100,10 +101,34 @@
// In case rules need to be slightly adjusted
@content;
}

// When a browser doesn't support :has, use focus-within as a fallback. This means that focus state is displayed on focus and not on focus-visible only (except some browsers like Safari).
@supports not selector(:has(:focus-visible)) {
&:is(:focus-visible, :focus-within, .pretend-focus)#{$vendor-prefix} {
outline-offset: spacing.$size-line;
outline: spacing.$size-line solid var(--post-focus-color);
border-radius: $border-radius;

@include high-contrast-mode() {
outline-color: Highlight;
}

// In case rules need to be slightly adjusted
@content;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a duplicated code, but I don't know how to improve that situation. We cannot use a placeholder here and the @content restrict the usage of another @mixin

}
}
}

@mixin focus-style-custom($vendor-prefix: '') {
&:is(:focus-visible, :focus-within, .pretend-focus)#{$vendor-prefix} {
// :has(:focus-visible) mimic a focus-visible-within pseudo-class
&:is(:focus-visible, :has(:focus-visible), .pretend-focus)#{$vendor-prefix} {
@content;
}

// When a browser doesn't support :has, use focus-within as a fallback. This means that focus state is displayed on focus and not on focus-visible only (except some browsers like Safari).
@supports not selector(:has(:focus-visible)) {
&:is(:focus-visible, :focus-within, .pretend-focus)#{$vendor-prefix} {
@content;
}
}
}
Loading