Skip to content

Commit

Permalink
feat(skip-link): href attr
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Sep 1, 2024
1 parent 28de41c commit b9a28ea
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .changeset/slick-ants-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@rhds/elements": patch
---
`<rh-skip-link>`: ensure link is always at top of the page, per guidelines
17 changes: 17 additions & 0 deletions .changeset/tall-corners-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@rhds/elements": minor
---
`<rh-skip-link>`: added optional `href` attribute:

```html
<rh-skip-link href="#main-content">Skip to main content</rh-skip-link>
```

is equivalent to:

```html
<rh-skip-link>
<a href="#main-content">Skip to main content</a>
</rh-skip-link>
```

8 changes: 8 additions & 0 deletions elements/rh-skip-link/demo/href-attribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<rh-skip-link href="#main-content">Skip to main content</rh-skip-link>

<h1>Skip link</h1>
<p>To show the skip link, click the white background and press your tab key.</p>

<script type="module">
import '@rhds/elements/rh-skip-link/rh-skip-link.js';
</script>
21 changes: 18 additions & 3 deletions elements/rh-skip-link/rh-skip-link.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#container {
display: block !important;
position: relative !important;
display: block;
z-index: 1000;
}

a#container {
position: fixed;
}

div#container {
position: relative;
}

a,
::slotted(a) {
background-color: var(--rh-color-surface-lightest, #ffffff) !important;
border-block-start-width: 0 !important;
Expand Down Expand Up @@ -31,7 +40,6 @@
outline: 0 !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
text-decoration: none !important;
top: -40px !important;
transform: translateX(-50%) !important;
Expand All @@ -40,6 +48,11 @@
width: 1px !important;
}

::slotted(a) {
position: fixed !important;
}

a:is(:active, :focus),
::slotted(a:is(:active, :focus)) {
border-width: var(--rh-border-width-md, 2px) !important;
clip: auto !important;
Expand All @@ -52,11 +65,13 @@
width: auto !important;
}

a:is(:active, :hover),
::slotted(a:is(:active, :hover)) {
text-decoration: underline !important;
}

@media (prefers-reduced-motion: reduce) {
a,
::slotted(a) {
transition-duration: 0.001ms !important;
}
Expand Down
16 changes: 10 additions & 6 deletions elements/rh-skip-link/rh-skip-link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';

import { property } from 'lit/decorators/property.js';

import styles from './rh-skip-link.css';

/**
Expand All @@ -10,19 +12,21 @@ import styles from './rh-skip-link.css';
*
* @summary Skip to the main content of a page
*
* @slot - Place an anchor tag with an `href` that targets an ID of the main content on the page.
* @slot - An anchor tag targeting the main page content by id hash.
* Or, if the `href` attribute is set, the text of the link.
*/
@customElement('rh-skip-link')
export class RhSkipLink extends LitElement {
static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };

static readonly styles = [styles];

@property({ reflect: true }) href?: string;

render() {
return html`
<div id="container">
<slot></slot>
</div>
`;
return this.href ?
html`<a id="container" href="${this.href}"><slot></slot></a>`
: html`<div id="container"><slot></slot></div>`;
}
}

Expand Down

0 comments on commit b9a28ea

Please sign in to comment.