Skip to content

Commit

Permalink
Refactor anchors
Browse files Browse the repository at this point in the history
* only show on hover
* move to the right on small screens
  • Loading branch information
bash committed Jul 22, 2024
1 parent 379312c commit b81fb76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
40 changes: 40 additions & 0 deletions sass/_anchor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Anchors are by default on the right
// but if there's enough space they get moved to the left
// and hang off into the empty space.

@mixin has-anchor {
.anchor {
position: absolute;
transition: opacity 125ms;
opacity: .5;

&:is(:hover, :focus) { opacity: 1; }
&:hover { text-decoration: none; }
}

@media (hover: hover) {
&:not(:is(:focus-within, :hover)) .anchor { opacity: 0; }
}
}

@mixin has-anchor-right {
&:has(.anchor) {
padding-right: 1.5ch;
}

.anchor {
right: 0;
}
}

@mixin has-anchor-left {
&:has(.anchor) {
padding-left: 1.5ch;
margin-left: -1.5ch;
}

.anchor {
left: 0;
right: auto;
}
}
5 changes: 2 additions & 3 deletions sass/_post-content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
// We only want the anchor hanging off to the left side
// if there's enough room available.
@media (width > ($content-max-width + 2rem)) {
:is(h1, h2, h3, h4) .anchor {
position: absolute;
transform: translateX(-100%) translateX(-0.5ch);
:is(h1, h2, h3, h4) {
@include has-anchor-left;
}
}
}
14 changes: 3 additions & 11 deletions sass/_typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ h1, h2, h3, h4 {
font-weight: bold;
text-wrap: balance;
text-wrap: pretty; // relatively new, so we have balance as fallback.
position: relative;

.anchor {
margin-right: 0.5ch;

@media (prefers-contrast: no-preference) and (hover: hover) {
transition: opacity 125ms;

&:not(:focus):not(:hover) {
opacity: .1;
}
}
}
@include has-anchor;
@include has-anchor-right;
}

h1 {
Expand Down
1 change: 1 addition & 0 deletions sass/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ $content-max-width: 50rem;
}
}

@import '_anchor.scss';
@import '_typography.scss';
@import '_layout.scss';
@import '_post-content.scss';
Expand Down

0 comments on commit b81fb76

Please sign in to comment.