@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
122 lines (107 loc) • 3.6 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import Select from "../Select";
import Stack from "../Stack";
import ButtonLink from "../Button";
import defaultTheme from "../defaultTheme";
import Translate from "../Translate";
const StyledNavigation = styled.div.withConfig({
displayName: "SkipNavigation__StyledNavigation",
componentId: "sc-1rj1ake-0"
})(["background-color:", ";padding:", ";width:100%;box-sizing:border-box;&:focus{outline:none;}", ";"], ({
theme
}) => theme.orbit.paletteCloudLight, ({
theme
}) => theme.orbit.spaceMedium, ({
show
}) => !show && css(["border:0;clip:rect(0 0 0 0);height:1px;margin:-1px;overflow:hidden;white-space:nowrap;padding:0;position:absolute;width:1px;"])); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledNavigation.defaultProps = {
theme: defaultTheme
};
const StyledSelectWrapper = styled.div.withConfig({
displayName: "SkipNavigation__StyledSelectWrapper",
componentId: "sc-1rj1ake-1"
})(["max-width:800px;"]);
const SkipNavigation = ({
actions,
feedbackUrl
}) => {
const [links, setLinks] = React.useState([]);
const [mappedLinks, setMappedLinks] = React.useState([]);
const [innerPages, setPages] = React.useState([]);
const [show, setShow] = React.useState(false);
const handleLinksClick = ev => {
const index = Number(ev.target.value);
const selected = links[index - 1];
if (selected) {
selected.setAttribute("tabindex", "-1");
selected.focus();
}
};
const handlePageClick = ev => {
if (actions) {
const index = Number(ev.target.value);
const selected = actions[index - 1];
if (selected.onClick) {
selected.onClick();
} else if (selected.link) {
window.location.href = selected.link;
}
}
};
const handleFocus = () => {
if (links.length === 0) {
const selectedLinks = document.querySelectorAll("[data-a11y-section]");
const mappedSections = [{
value: 0,
label: "Jump to section" // TODO: Dictionary
}, ...Object.keys(selectedLinks).map(key => ({
value: Number(key) + 1,
label: selectedLinks[Number(key)].innerText
}))];
if (selectedLinks) {
setLinks(selectedLinks);
}
setMappedLinks(mappedSections);
if (actions) {
const mappedPages = [{
value: 0,
label: "Common actions" // TODO: Dictionary
}, ...actions.map((el, i) => {
return {
value: i + 1,
label: el.name
};
})];
setPages(mappedPages);
}
}
setShow(true);
};
return /*#__PURE__*/React.createElement(StyledNavigation, {
tabIndex: "-1",
onFocus: handleFocus,
onBlur: () => setShow(false),
show: show
}, /*#__PURE__*/React.createElement(Stack, {
justify: "between"
}, /*#__PURE__*/React.createElement(StyledSelectWrapper, null, /*#__PURE__*/React.createElement(Stack, {
align: "center"
}, /*#__PURE__*/React.createElement(Select, {
options: mappedLinks,
onChange: handleLinksClick,
size: "small"
}), innerPages.length > 0 && /*#__PURE__*/React.createElement(Select, {
options: innerPages,
onChange: handlePageClick,
size: "small"
}))), feedbackUrl && /*#__PURE__*/React.createElement(ButtonLink, {
href: feedbackUrl,
type: "secondary",
external: true,
size: "small"
}, /*#__PURE__*/React.createElement(Translate, {
tKey: "a11ymenu_send_feedback"
}))));
};
export default SkipNavigation;