You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import { translate } from '../../helpers/l10n';
  22. import { A11ySkipLink } from '../../types/types';
  23. import { A11yContext } from './A11yContext';
  24. interface Props {
  25. anchor: string;
  26. label?: string;
  27. weight?: number;
  28. }
  29. export default function A11ySkipTarget(props: Props) {
  30. return (
  31. <A11yContext.Consumer>
  32. {({ addA11ySkipLink, removeA11ySkipLink }) => (
  33. <A11ySkipTargetInner
  34. addA11ySkipLink={addA11ySkipLink}
  35. removeA11ySkipLink={removeA11ySkipLink}
  36. {...props}
  37. />
  38. )}
  39. </A11yContext.Consumer>
  40. );
  41. }
  42. interface InnerProps {
  43. addA11ySkipLink: (link: A11ySkipLink) => void;
  44. removeA11ySkipLink: (link: A11ySkipLink) => void;
  45. }
  46. export class A11ySkipTargetInner extends React.PureComponent<Props & InnerProps> {
  47. componentDidMount() {
  48. this.props.addA11ySkipLink(this.getLink());
  49. }
  50. componentWillUnmount() {
  51. this.props.removeA11ySkipLink(this.getLink());
  52. }
  53. getLink = (): A11ySkipLink => {
  54. const { anchor: key, label = translate('skip_to_content'), weight } = this.props;
  55. return { key, label, weight };
  56. };
  57. render() {
  58. const { anchor } = this.props;
  59. return <span id={`a11y_target__${anchor}`} />;
  60. }
  61. }