Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1488 from navikt/uu-fix-skiplink
Browse files Browse the repository at this point in the history
skiplink som link + aria-labelled-by for å unngå dobbelt opplesning i SR
  • Loading branch information
PerOlavM authored Sep 14, 2023
2 parents 0298a0d + 317640d commit 47d8c9c
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/komponenter/header/common/skiplinks/Skiplink.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import React, { useEffect, useState } from 'react';
import { logAmplitudeEvent } from 'utils/analytics/amplitude';
import React, {useEffect, useState} from 'react';
import { LenkeMedSporing } from 'komponenter/common/lenke-med-sporing/LenkeMedSporing';
import { AnalyticsEventArgs, AnalyticsCategory } from 'utils/analytics/analytics';
import Tekst from 'tekster/finn-tekst';

import style from './Skiplink.module.scss';

const mainContentId = 'maincontent';
const mainContentHref = `#${mainContentId}`;
const skiplinkTextId = 'skiplink-text';

const Skiplink = () => {
const [hasMainContent, setHasMainContent] = useState(false);
const linkTextId = 'skiplinks-ga-til-hovedinnhold';
const anlyticsProps:AnalyticsEventArgs = {
category: AnalyticsCategory.Header,
eventName: 'skiplinks',
action: linkTextId,
destination: mainContentHref,
}

useEffect(() => {
const mainContentElement = document.getElementById(mainContentId);
setHasMainContent(!!mainContentElement);
}, []);

return hasMainContent ? (
<nav aria-label="Hopp til innhold">
<button
<nav aria-labelledby={skiplinkTextId}>
<LenkeMedSporing
href={mainContentHref}
className={style.skiplink}
onClick={() => {
logAmplitudeEvent('skiplinks', { kilde: 'header', fritekst: linkTextId });
document.getElementById(mainContentId)?.focus();
}}
analyticsEventArgs={anlyticsProps}
>
<Tekst id={linkTextId} />
</button>
<span id={skiplinkTextId}>
<Tekst id={linkTextId} />
</span>
</LenkeMedSporing>
</nav>
) : null;
};
Expand Down

0 comments on commit 47d8c9c

Please sign in to comment.