Skip to content

Commit 0dc5b8c

Browse files
committed
Refactor anchors
* only show on hover * move to the right on small screens
1 parent 564151a commit 0dc5b8c

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

sass/_anchor.scss

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Anchors are by default on the right
2+
// but if there's enough space they get moved to the left
3+
// and hang off into the empty space.
4+
5+
@mixin has-anchor {
6+
.anchor {
7+
position: absolute;
8+
transition: opacity 125ms;
9+
opacity: .5;
10+
11+
&:is(:hover, :focus) { opacity: 1; }
12+
&:hover { text-decoration: none; }
13+
}
14+
15+
@media (hover: hover) {
16+
&:not(:is(:focus-within, :hover)) .anchor { opacity: 0; }
17+
}
18+
}
19+
20+
@mixin has-anchor-right {
21+
&:has(.anchor) {
22+
padding-right: 1.5ch;
23+
}
24+
25+
.anchor {
26+
right: 0;
27+
}
28+
}
29+
30+
@mixin has-anchor-left {
31+
&:has(.anchor) {
32+
padding-left: 1.5ch;
33+
margin-left: -1.5ch;
34+
}
35+
36+
.anchor {
37+
left: 0;
38+
right: auto;
39+
}
40+
}

sass/_post-content.scss

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@
4545
// We only want the anchor hanging off to the left side
4646
// if there's enough room available.
4747
@media (width > ($content-max-width + 2rem)) {
48-
:is(h1, h2, h3, h4) .anchor {
49-
position: absolute;
50-
transform: translateX(-100%) translateX(-0.5ch);
48+
:is(h1, h2, h3, h4) {
49+
@include has-anchor-left;
5150
}
5251
}
5352
}

sass/_typography.scss

+3-11
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ h1, h2, h3, h4 {
55
font-weight: bold;
66
text-wrap: balance;
77
text-wrap: pretty; // relatively new, so we have balance as fallback.
8+
position: relative;
89

9-
.anchor {
10-
margin-right: 0.5ch;
11-
12-
@media (prefers-contrast: no-preference) and (hover: hover) {
13-
transition: opacity 125ms;
14-
15-
&:not(:focus):not(:hover) {
16-
opacity: .1;
17-
}
18-
}
19-
}
10+
@include has-anchor;
11+
@include has-anchor-right;
2012
}
2113

2214
h1 {

sass/style.scss

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ $content-max-width: 50rem;
3535
}
3636
}
3737

38+
@import '_anchor.scss';
3839
@import '_typography.scss';
3940
@import '_layout.scss';
4041
@import '_post-content.scss';

0 commit comments

Comments
 (0)