-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
import Tooltip from '@material-ui/core/Tooltip' | ||
import { LinkableContext } from '.' | ||
import CopyToClipboard from '../CopyToClipboard' | ||
import { IconContainer } from '../styled' | ||
|
||
const HashContainer = styled.div` | ||
display: flex; | ||
position: relative; | ||
bottom: 5px; | ||
right: 9px; | ||
cursor: pointer; | ||
` | ||
|
||
export default function CopyLink({ tooltip = 'Copy Metric link' }) { | ||
const { id } = React.useContext(LinkableContext) | ||
|
||
return id ? ( | ||
<HashContainer> | ||
<CopyToClipboard> | ||
{({ copy }) => ( | ||
<IconContainer> | ||
<Tooltip title={tooltip} arrow> | ||
<span | ||
onClick={() => { | ||
copy(window.location.origin + window.location.pathname + `#${id}`) | ||
}} | ||
> | ||
# | ||
</span> | ||
</Tooltip> | ||
</IconContainer> | ||
)} | ||
</CopyToClipboard> | ||
</HashContainer> | ||
) : null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react' | ||
import { useLocation } from 'react-router-dom' | ||
import { GlowingWrapper } from '../styled' | ||
|
||
export const LinkableContext = React.createContext({ active: false, onInitCallback: fn => {}, id: '' }) | ||
|
||
export default function LinkableComponent({ children, id }) { | ||
const location = useLocation() | ||
const wrapperRef = React.useRef<HTMLElement>(null) | ||
const [initialized, setInitialized] = React.useState(false) | ||
const glow = location.hash === `#${id}` | ||
const onInitCallback = React.useCallback( | ||
(fn: Function) => { | ||
if (initialized) return | ||
|
||
fn() | ||
setInitialized(true) | ||
}, | ||
[initialized], | ||
) | ||
const value = React.useMemo( | ||
() => ({ | ||
active: glow, | ||
onInitCallback, | ||
id, | ||
}), | ||
[glow, onInitCallback, id], | ||
) | ||
|
||
React.useEffect(() => { | ||
if (glow && wrapperRef && wrapperRef.current) { | ||
wrapperRef.current.scrollIntoView({ behavior: 'smooth' }) | ||
} | ||
}, [glow]) | ||
|
||
return ( | ||
<LinkableContext.Provider value={value}> | ||
<GlowingWrapper ref={wrapperRef} glow={glow}> | ||
{children} | ||
</GlowingWrapper> | ||
</LinkableContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters