Skip to content

Commit

Permalink
fix clicking permalink icon toggling collapser
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyzanchi committed Dec 21, 2023
1 parent 0867ce6 commit 936aaae
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/gatsby-theme-newrelic/src/components/Collapser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, useMemo } from 'react';
import React, { useEffect, useState, useMemo, useRef } from 'react';
import PropTypes from 'prop-types';
import { css, keyframes } from '@emotion/react';
import styled from '@emotion/styled';
Expand Down Expand Up @@ -28,6 +28,7 @@ const Collapser = ({ title, id, defaultOpen, children }) => {
const { height: viewHeight } = useSpring({ height: isOpen ? height : 0 });
const previousIsOpen = usePrevious(isOpen);
const [copied, copy] = useClipboard();
const linkRef = useRef(null);

useKeyPress(['s', 'f', 'h'], (e) => setIsOpen(e.key !== 'h'));

Expand Down Expand Up @@ -74,7 +75,14 @@ const Collapser = ({ title, id, defaultOpen, children }) => {
`}
>
<button
onClick={() => setIsOpen((isOpen) => !isOpen)}
onClick={(e) => {
if (
linkRef.current?.contains(e.target) ||
linkRef.current === e.target
)
return;
setIsOpen((isOpen) => !isOpen);
}}
type="button"
css={css`
--color-transition-duration: 0.3s;
Expand Down Expand Up @@ -121,6 +129,7 @@ const Collapser = ({ title, id, defaultOpen, children }) => {
<span>{title}</span>
{id && (
<Link
ref={linkRef}
to={`#${id}`}
className="anchor"
css={css`
Expand Down

0 comments on commit 936aaae

Please sign in to comment.