Skip to content

Commit 28f2cb3

Browse files
Add CSS Focus Article
1 parent dae79b6 commit 28f2cb3

File tree

10 files changed

+213
-63
lines changed

10 files changed

+213
-63
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
const { focusType } = Astro.props
3+
---
4+
5+
<button class={focusType}><slot /></button>
6+
7+
<style>
8+
button {
9+
border: none;
10+
border-radius: 0.25em;
11+
padding: 0.5em 0.75em;
12+
font-size: inherit;
13+
background: var(--theme-blue);
14+
cursor: pointer;
15+
}
16+
17+
.focus:focus {
18+
background: var(--theme-red);
19+
}
20+
21+
.focus-visible:focus-visible {
22+
background: var(--theme-red);
23+
}
24+
</style>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
const { focusType } = Astro.props
3+
---
4+
5+
<div class={focusType}><slot /></div>
6+
7+
<style>
8+
div {
9+
border: 2px solid var(--theme-blue);
10+
border-radius: 0.25rem;
11+
padding: 1rem;
12+
display: flex;
13+
gap: 1rem;
14+
15+
@media (max-width: 700px) {
16+
flex-direction: column;
17+
}
18+
}
19+
20+
.focus-within:focus-within {
21+
border-color: var(--theme-red);
22+
}
23+
24+
.focus-visible-within:has(:focus-visible) {
25+
border-color: var(--theme-red);
26+
}
27+
</style>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
const { focusType, placeholder } = Astro.props
3+
---
4+
5+
<input placeholder={placeholder} class={focusType} />
6+
7+
<style>
8+
input {
9+
background: var(--theme-code-inline-bg);
10+
border: 2px solid var(--theme-blue);
11+
border-radius: 0.25em;
12+
padding: 0.25em 0.5em;
13+
font-size: inherit;
14+
color: inherit;
15+
outline: none;
16+
}
17+
18+
.focus:focus {
19+
border-color: var(--theme-red);
20+
}
21+
22+
.focus-visible:focus-visible {
23+
border-color: var(--theme-red);
24+
}
25+
</style>

src/components/BlogPost.astro

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import ShareButtons from "/src/components/ShareButtons.jsx"
66
export interface Props {
77
title: string
88
date: string
9+
updatedDate: string
910
url: string
1011
tags: string[]
1112
}
1213
13-
const { title, date, tags, url } = Astro.props
14+
const { title, date, updatedDate, tags, url } = Astro.props
1415
const siteUrl = Astro.site.href
1516
---
1617

@@ -19,7 +20,16 @@ const siteUrl = Astro.site.href
1920
<div>
2021
<header>
2122
<h1 class="title">{title}</h1>
22-
<p class="publish-date">{dateFormatter.format(new Date(date))}</p>
23+
<p class={`publish-date ${updatedDate && "old-date"}`}>
24+
{dateFormatter.format(new Date(date))}
25+
</p>
26+
{
27+
updatedDate && (
28+
<p class="publish-date">
29+
Updated: {dateFormatter.format(new Date(updatedDate))}
30+
</p>
31+
)
32+
}
2333
<div style="margin-bottom: 1rem">
2434
<TagBar
2535
tags={tags.map(tag => {
@@ -75,6 +85,11 @@ const siteUrl = Astro.site.href
7585
color: var(--theme-text-lighter);
7686
}
7787

88+
.publish-date.old-date {
89+
font-size: 0.9rem;
90+
text-decoration: line-through;
91+
}
92+
7893
.title {
7994
font-size: 2.25rem;
8095
font-weight: 700;

src/components/BlogPostPreview.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ export default function BlogPostPreview({ post }) {
2424
margin: 0,
2525
fontSize: "1rem",
2626
color: "var(--theme-text-lighter)",
27+
display: "flex",
28+
flexDirection: "column",
2729
}}
2830
>
29-
{dateFormatter.format(post.date)}
31+
<span
32+
style={{
33+
textDecoration: post.updatedDate ? "line-through" : "none",
34+
}}
35+
>
36+
{dateFormatter.format(post.date)}
37+
</span>
38+
{post.updatedDate && (
39+
<>Updated: {dateFormatter.format(post.updatedDate)}</>
40+
)}
3041
</p>
3142
<TagBar
3243
marginTop=".25em"

src/layouts/BlogPost.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
title,
1212
description,
1313
date,
14+
updatedDate,
1415
author = "Kyle Cook",
1516
permalink,
1617
tags,
@@ -26,7 +27,7 @@ const url = Astro.request.url
2627
<body>
2728
<BaseTopOfBody />
2829
<div class="wrapper">
29-
<BlogPost {title} {author} {date} {tags} {url}>
30+
<BlogPost {title} {author} {date} {updatedDate} {tags} {url}>
3031
<slot />
3132
</BlogPost>
3233
</div>

src/pages/2022-09/css-has-selector/index.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
layout: "@layouts/BlogPost.astro"
33
title: "CSS :has A Parent Selector And It Is Better Than I Ever Could Have Hoped For"
44
date: "2022-09-26"
5+
updatedDate: "2024-07-29"
56
description: "CSS has been lacking a parent selector for years and now they finally introduced one with the :has pseudo class and it is incredible!"
67
tags: ["CSS"]
78
---
@@ -115,7 +116,7 @@ This selector has an overall specificity of 2 classes and 1 element since the fi
115116

116117
## Browser Support
117118

118-
With every cool CSS feature you always have to consider browser support, but luckily the browser support for this new property is quite good. It may seem low considering it only has [56.2% support](https://caniuse.com/css-has), but this is a bit misleading since every major browser other than Firefox has support for this feature. The reason for the low percentage is because this features just launched at the end of August 2022 in Chrome so people are still in the process of updating Chrome to the latest versions. Firefox also has this feature behind a feature flag which hopefully means it is coming to the browser very soon. In just a few months this feature should be above 90% and able to be used on pretty much every site.
119+
The `:has` selector is supported in all major browsers as of the end of 2023.
119120

120121
## Conclusion
121122

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,149 @@
11
---
22
layout: "@layouts/BlogPost.astro"
3-
title: "We Can Finally Animate height: auto; in CSS!"
4-
date: "2024-07-01"
5-
description: "For decades, height: auto; has been a pain to animate in CSS. But with the introduction of the calc-size() function in CSS, we can finally animate height: auto; without the need for any JavaScript."
3+
title: "Do You Know All 4 CSS Focus Styles?"
4+
date: "2024-07-29"
5+
description: "Handling focus in CSS is quite a bit more complicated than it seems and in this article I cover the 3 main CSS focus methods as well as a secret fourth focus method."
66
tags: ["CSS"]
77
---
88

9-
import CalcSizeComparison from "@blogComponents/cssCalcSize/CalcSizeComparison.astro"
9+
import CSSFocusButton from "@blogComponents/cssFocusCrashCourse/CssFocusButton.astro"
10+
import CSSFocusInput from "@blogComponents/cssFocusCrashCourse/CSSFocusInput.astro"
11+
import CSSFocusContainer from "@blogComponents/cssFocusCrashCourse/CSSFocusContainer.astro"
12+
import Tangent from "@blogComponents/lib/Tangent.astro"
1013

1114
## Introduction
1215

13-
Animating `height: auto;` in CSS seems like it should be easy, but CSS is unable to animate to/from `height: auto;` since it needs a specific height value to run any animation/transition. This has been a pain point for web developers for decades, and the only way to animate `height: auto;` was to use JavaScript to calculate the height of the element and then animate it. This is obviously not ideal, which is why CSS has finally added the brand new `calc-size()` function which makes this type of animation trivial.
16+
It may seem strange to write an entire article on handling focus in CSS since it is a pretty simple property at first glance. That is until you realize that CSS actually has 3 (plus one secret) focus methods that all behave differently. These properties even have different behaviors depending on what elements you use them on which makes it even more confusing. In this article I will be breaking down all 4 focus methods, showing you when to use each one, and how they differ from each other.
1417

15-
## `calc-size()`
18+
## What Is Focus?
1619

17-
The `calc-size()` function works exactly the same as the `calc()` function, but it has the additional capability of calculating based on sizes that are automatically calculated by the browser. These values are:
20+
Before we dive into each focus method we need to understand what a focus state is. When an element is focused it means that it is currently selected by the user. This can happen when a user clicks on an element, uses the tab key to navigate through a page, or even when a user interacts with an element using a screen reader.
1821

19-
1. `auto`
20-
2. `min-content`
21-
3. `max-content`
22-
4. `fit-content`
23-
5. `stretch`
24-
6. `contain`
22+
The focus state is important for accessibility reasons as it helps users understand where they are on a page and what they are interacting with. Imagine trying to fill out a form and not knowing which input you are currently typing in. This is what the web would be like if there were no focus states.
2523

26-
Essentially, what this function does is convert values like `auto` to specific pixel values which it can then use in calculations with other values. This is handy on its own, but where it is most useful is with animating elements that are `auto` sized.
24+
## `:focus`
2725

28-
```css {8}
29-
.element {
30-
height: 0;
31-
overflow: hidden;
32-
transition: height 0.3s;
26+
The main focus method is the `:focus` pseudo-class. This is the what most people think of when they think of focus styles in CSS, but this pseudo-class has quite a few problems which is why I almost never use it.
27+
28+
```css {5}
29+
button {
30+
background-color: blue;
3331
}
3432

35-
.element.open {
36-
height: calc-size(auto);
33+
button:focus {
34+
background-color: red;
3735
}
3836
```
3937

40-
By wrapping our `auto` value in the `calc-size()` function, we can now animate the height of the element from `0` to `auto` without any JavaScript. Here is an example of what this looks like in practice:
38+
The styles within the `:focus` pseudo-class will be applied whenever an element is focused. It doesn't matter how the element was focused (clicked on, keyboard navigation, screen readers, etc.) it will always show the focus styles. You can see this in action in the example below.
39+
40+
<CSSFocusButton focusType="focus">Focus Me</CSSFocusButton>
4141

42-
<CalcSizeComparison />
42+
As you can see it doesn't matter how you focus this button it will always turn red. This is generally not ideal since when you click on a button you don't generally want it to show focus styles after you have finished clicking on it, but you do want the focus state to persist when using keyboard navigation since this focus state tells the user where they are on the page.
4343

44-
The only thing you need to be away of is that you cannot animate between two automatically calculated values, such as `auto` and `min-content`.
44+
<Tangent>
45+
If you are having trouble focusing on the button with the keyboard try
46+
clicking on the text just above the button and then clicking the
47+
<kbd>Tab</kbd> key.
48+
</Tangent>
4549

46-
Another interesting thing about `calc-size()` is you can actually use it on the non-automatic value in the animation and it will still animate correctly. As long as you have `calc-size` on one of the values in the animation, it will work.
50+
## `:focus-visible`
4751

48-
```css {3}
49-
.element {
50-
/* This still works */
51-
height: calc-size(0px);
52-
overflow: hidden;
53-
transition: height 0.3s;
52+
I mentioned how `:focus` is not always ideal since it shows the focus styles no matter how the element was focused. This is where the `:focus-visible` pseudo-class comes in. This pseudo-class is a bit smarter than `:focus` since it only shows the focus styles when the browser deems the user needs those styles to know where they are on the page and what element they currently have focused. I almost always use `:focus-visible` over `:focus` since it provides a better user experience.
53+
54+
```css {5}
55+
button {
56+
background-color: blue;
5457
}
5558

56-
.element.open {
57-
height: auto;
59+
button:focus-visible {
60+
background-color: red;
5861
}
5962
```
6063

61-
### Doing Actual Calculations
64+
<CSSFocusButton focusType="focus-visible">
65+
Focus Me With Keyboard
66+
</CSSFocusButton>
67+
68+
As you can see the above button above does not show any focus styles when it is clicked, but if you use the keyboard to navigate to this element it will show the focus styles.
6269

63-
By far the most common use for this will be with animations/transitions as shown above, but since this function works just like `calc` it can actually be used to do certain calculations that used to be impossible.
70+
<Tangent>
71+
If you look closely you may notice that there is a ring that appears around
72+
the button when you use keyboard navigation to focus it. This is part of the
73+
default browser styles and is called the `outline` property. The browser
74+
applies this `outline` property using the `:focus-within` pseudo-class which
75+
is why it only appears when needed to assist the user in knowing where they
76+
are on the page.
77+
</Tangent>
78+
79+
One thing to note about `:focus-visible` is that it works differently when used on different elements. For example, if you use `:focus-visible` on an input element it will show the focus style no matter what. It doesn't matter if you click the input or use keyboard navigation it will always show the focus styles. This is because the browser deems it important to show the focus styles on input elements no matter how they are focused.
80+
81+
```css {5}
82+
input {
83+
border-color: blue;
84+
}
6485

65-
```css
66-
.element {
67-
width: calc-size(min-content, size + 50px);
86+
input:focus-visible {
87+
border-color: red;
6888
}
6989
```
7090

71-
The above CSS will set the width of the element to the minimum content size plus `50px`. The syntax for this is a bit confusing so let me explain.
91+
<CSSFocusInput focusType="focus-visible" placeholder="Focus Me" />
7292

73-
`calc-size` takes two arguments, the first is the size that you want to calculate, and the second is the calculation you want to perform. In this case, we are calculating the `min-content` size of the element and then adding `50px` to that value. The keyword `size` is always used to represent the current size of the first property passed to `calc-size`. This means in our example `size` would be equal to the `min-content` size of the element.
93+
## `:focus-within`
7494

75-
You can even nest multiple `calc-size` functions to perform more complex calculations.
95+
The `:focus-within` pseudo-class is a bit different than the previous two focus methods. This pseudo-class is used to apply styles to a parent element based on the `:focus` state of its children. If any of the children would show the `:focus` state then the parent will also show the `:focus-within` state.
7696

77-
```css
78-
.element {
79-
width: calc-size(calc-size(min-content, size + 50px), size * 2);
97+
```css {5}
98+
.container {
99+
border-color: blue;
100+
}
101+
102+
.container:focus-within {
103+
border-color: red;
80104
}
81105
```
82106

83-
This will calculate the `min-content` size of the element, add `50px` to that value, and then multiply the result by `2`.
107+
<CSSFocusContainer focusType="focus-within">
108+
<CSSFocusButton focusType="focus">:focus</CSSFocusButton>
109+
<CSSFocusButton focusType="focus-visible">:focus-visible</CSSFocusButton>
110+
<CSSFocusButton>No Focus Styles</CSSFocusButton>
111+
</CSSFocusContainer>
112+
113+
In the above example there is container that will show a red border anytime one of its children is focused. There are 3 buttons in that container. The first has a `:focus` style, the second has a `:focus-visible` style, and the third has no focus styles. As you can see it doesn't matter which button you click the container will always show the `:focus-within` styles since one of its children is focused.
84114

85-
## Browser Support
115+
It is important to know that the child elements do not need to have a focus style defined for the parent to show the `:focus-within` state. The parent will show the `:focus-within` state as long as one of its children is focused. This works with any element that can be focused, not just buttons.
86116

87-
This is where we get to the bad news. As of writing this article, `calc-size()` is only supported in Chrome Canary when the `#enable-experimental-web-platform-features` flag is enabled. It is so new there isn't even a caniuse.com page for me to link to yet.
117+
## The Secret Fourth Focus Method
88118

89-
Luckily, this CSS feature is not something that will break your site if it isn't supported. It will just mean that the animation won't work, so you can use it today and it will act as a progressive enhancement for users on browsers that support it.
119+
The final focus method is a bit different since there is no built in CSS pseudo class for `:focus-visible-within`. Instead we have to write our own custom CSS selector that does the same thing. This custom selector combines hwo `:focus-within` and `:focus-visible` work to create a focus method that only shows focus styles when a child of the element would have its `:focus-visible` styles shown.
90120

91-
```css {8-9}
92-
.element {
93-
height: 0;
94-
overflow: hidden;
95-
transition: height 0.3s;
121+
```css {5}
122+
.container {
123+
border-color: blue;
96124
}
97125

98-
.element.open {
99-
height: auto;
100-
height: calc-size(auto);
126+
.container:has(:focus-visible) {
127+
border-color: red;
101128
}
102129
```
103130

104-
With the above CSS the animation will work in browsers that support `calc-size()` while in older browsers it will just show the element without any animation.
131+
<CSSFocusContainer focusType="focus-visible-within">
132+
<CSSFocusButton focusType="focus">:focus</CSSFocusButton>
133+
<CSSFocusButton focusType="focus-visible">:focus-visible</CSSFocusButton>
134+
<CSSFocusButton>No Focus Styles</CSSFocusButton>
135+
<CSSFocusInput focusType="focus" placeholder=":focus" />
136+
</CSSFocusContainer>
137+
138+
I have pretty much the same code in this example as the previous example, but I added a single input element to the container. This input element has a `:focus` style applied to it. This container will only show the custom `:focus-visible-within` styles when I use the keyboard to navigate to any of the buttons or when I focus the input in any way. If I click on any of the buttons the container will not show the custom `:focus-visible-within` styles.
139+
140+
The way this `:focus-visible-within` works is by using the `:has` pseudo element to only select the `.container` if at least one child element has the `:focus-visible` pseudo-class applied to it.
141+
142+
<Tangent>
143+
If you want to learn more about the `:has` selector and why I love it you can
144+
check out my complete [:has selector article](/2022-09/css-has-selector).
145+
</Tangent>
105146

106147
## Conclusion
107148

108-
`calc-size()` is a fantastic new addition to CSS that will make animating `auto` based sizes incredibly easy. It also opens up a lot of possibilities for doing calculations that were previously impossible in CSS. I can't wait for this feature to be supported in all browsers!
149+
Hopefully this article has helped you understand how each focus method differs from the others and when each should be used. Personally, I almost never use `:focus` as `:focus-visible` is almost always a better user experience. I don't tend to use `:focus-within` or the custom `:focus-visible-within` as much, but when you need them they are incredibly useful.

0 commit comments

Comments
 (0)