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 d6434fe commit 154644c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
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>
4 changes: 4 additions & 0 deletions elements/rh-skip-link/rh-skip-link.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
position: relative !important;
}

a,
::slotted(a) {
background-color: var(--rh-color-surface-lightest, #ffffff) !important;
border-block-start-width: 0 !important;
Expand Down Expand Up @@ -40,6 +41,7 @@
width: 1px !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 +54,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
13 changes: 10 additions & 3 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,17 +12,22 @@ 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 id="container">${this.href ? html`
<a href="${this.href}"><slot></slot></a>` : html`
<slot></slot>`}
</div>
`;
}
Expand Down

0 comments on commit 154644c

Please sign in to comment.