-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor remaining Documentation components to TS
- Loading branch information
Showing
35 changed files
with
1,297 additions
and
1,287 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
133 changes: 133 additions & 0 deletions
133
src/components/Documentation/Markdown/Tooltip/DesktopView/index.tsx
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,133 @@ | ||
import React, { useRef, useState, useEffect } from 'react' | ||
import cn from 'classnames' | ||
import ReactMarkdown from 'react-markdown' | ||
import throttle from 'lodash.throttle' | ||
|
||
import Portal from '../Portal' | ||
import { getHeaderHeight } from '../../../../../utils/scroll' | ||
import styles from './styles.module.css' | ||
|
||
interface IDesktopViewProps { | ||
description: string | ||
header: string | ||
text: React.ReactNode | ||
} | ||
|
||
interface ITooltipPosition { | ||
left: number | ||
top: number | ||
arrow: ['l' | 'r', 't' | 'b'] | ||
} | ||
|
||
const ARROW_SIZE = 10 | ||
|
||
const getPosition = (toggle: Element, tooltip: Element): ITooltipPosition => { | ||
const toggleRect = toggle.getBoundingClientRect() | ||
const tooltipRect = tooltip.getBoundingClientRect() | ||
const windowWidth = document.documentElement.clientWidth | ||
const headerHeight = getHeaderHeight() | ||
const result: ITooltipPosition = { left: 0, top: 0, arrow: ['l', 'b'] } | ||
|
||
if (windowWidth - tooltipRect.width > toggleRect.left) { | ||
result.left = toggleRect.left | ||
} else { | ||
result.left = toggleRect.left + toggleRect.width - tooltipRect.width | ||
result.arrow[0] = 'r' | ||
} | ||
|
||
if (toggleRect.top > tooltipRect.height + ARROW_SIZE + headerHeight) { | ||
result.top = toggleRect.top - tooltipRect.height - ARROW_SIZE | ||
} else { | ||
result.top = toggleRect.top + toggleRect.height + ARROW_SIZE | ||
result.arrow[1] = 't' | ||
} | ||
|
||
return result | ||
} | ||
|
||
const DesktopView: React.SFC<IDesktopViewProps> = ({ | ||
description, | ||
header, | ||
text | ||
}) => { | ||
const timeoutRef = useRef<number | undefined>() | ||
const toggleRef = useRef<HTMLSpanElement>(null) | ||
const tooltipRef = useRef<HTMLDivElement>(null) | ||
const [tooltipPosition, setPosition] = useState< | ||
ITooltipPosition | undefined | ||
>() | ||
const [isVisible, setVisible] = useState(false) | ||
const calcPosition = () => { | ||
if (!tooltipRef.current || !toggleRef.current) { | ||
return | ||
} | ||
|
||
setPosition(getPosition(toggleRef.current, tooltipRef.current)) | ||
} | ||
const throttledCalcPosition = throttle(calcPosition, 50) | ||
const show = () => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
timeoutRef.current = undefined | ||
} | ||
|
||
setVisible(true) | ||
} | ||
const hide = () => { | ||
timeoutRef.current = window.setTimeout(() => setVisible(false), 100) | ||
} | ||
|
||
useEffect(() => { | ||
document.addEventListener('scroll', throttledCalcPosition) | ||
window.addEventListener('resize', throttledCalcPosition) | ||
|
||
return () => { | ||
document.removeEventListener('scroll', throttledCalcPosition) | ||
window.removeEventListener('resize', throttledCalcPosition) | ||
} | ||
}, []) | ||
useEffect(() => { | ||
if (!tooltipRef.current) { | ||
return | ||
} | ||
|
||
calcPosition() | ||
}, [isVisible, tooltipRef.current]) | ||
|
||
return ( | ||
<> | ||
{isVisible && ( | ||
<Portal> | ||
<div | ||
ref={tooltipRef} | ||
className={cn( | ||
styles.tooltip, | ||
tooltipPosition?.arrow && styles.calculated, | ||
tooltipPosition?.arrow && styles[tooltipPosition.arrow.join('')] | ||
)} | ||
style={tooltipPosition} | ||
onMouseOver={show} | ||
onMouseLeave={hide} | ||
onFocus={show} | ||
onBlur={hide} | ||
> | ||
<div className={styles.tooltipHeader}>{header}</div> | ||
<ReactMarkdown className="markdown-body" source={description} /> | ||
</div> | ||
</Portal> | ||
)} | ||
<span | ||
ref={toggleRef} | ||
className={styles.highlightedText} | ||
onMouseOver={show} | ||
onMouseLeave={hide} | ||
onFocus={show} | ||
onBlur={hide} | ||
> | ||
{text} | ||
</span> | ||
</> | ||
) | ||
} | ||
|
||
export default DesktopView |
68 changes: 68 additions & 0 deletions
68
src/components/Documentation/Markdown/Tooltip/DesktopView/styles.module.css
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,68 @@ | ||
.highlightedText { | ||
border-bottom: 1px black dotted; | ||
} | ||
|
||
.tooltip { | ||
position: absolute; | ||
display: none; | ||
width: 400px; | ||
padding: 8px 10px; | ||
border: 1px solid var(--color-lighter-blue); | ||
border-radius: 3px; | ||
color: var(--color-black); | ||
background-color: #fff; | ||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); | ||
|
||
&.calculated { | ||
display: block; | ||
} | ||
|
||
&::after { | ||
position: absolute; | ||
content: ''; | ||
display: block; | ||
height: 10px; | ||
width: 10px; | ||
border: none; | ||
border-top: 1px solid var(--color-lighter-blue); | ||
border-left: 2px solid var(--color-lighter-blue); | ||
transform: rotate(90deg); | ||
background-color: #fff; | ||
} | ||
|
||
&.lt::after, | ||
&.rt::after { | ||
top: -7px; | ||
border-top-width: 2px; | ||
border-left-width: 1px; | ||
transform: rotate(45deg); | ||
} | ||
|
||
&.lt::after { | ||
left: 27px; | ||
} | ||
|
||
&.rt::after { | ||
right: 27px; | ||
} | ||
|
||
&.lb::after, | ||
&.rb::after { | ||
top: 100%; | ||
margin-top: -4px; | ||
transform: rotate(-135deg); | ||
} | ||
|
||
&.lb::after { | ||
left: 27px; | ||
} | ||
|
||
&.rb::after { | ||
right: 27px; | ||
} | ||
} | ||
|
||
.tooltipHeader { | ||
font-size: 1.3em; | ||
font-weight: bold; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/components/Documentation/Markdown/Tooltip/MobileView/index.tsx
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,79 @@ | ||
import React, { useState } from 'react' | ||
import cn from 'classnames' | ||
import ReactMarkdown from 'react-markdown' | ||
|
||
import Portal from '../Portal' | ||
|
||
import styles from './styles.module.css' | ||
|
||
interface IMobileViewProps { | ||
description: string | ||
header: string | ||
text: React.ReactNode | ||
} | ||
|
||
const MobileView: React.SFC<IMobileViewProps> = ({ | ||
description, | ||
header, | ||
text | ||
}) => { | ||
const [isVisible, setVisible] = useState(false) | ||
const openTooltip = (e: React.MouseEvent<HTMLSpanElement>) => { | ||
e.stopPropagation() | ||
setVisible(true) | ||
} | ||
const onOpenKeyDown = (e: React.KeyboardEvent) => { | ||
if (e.which === 13 || e.which === 32) { | ||
setVisible(true) | ||
} | ||
} | ||
const closeTooltip = (e: React.MouseEvent<HTMLDivElement>) => { | ||
e.stopPropagation() | ||
setVisible(false) | ||
} | ||
const onCloseKeyDown = (e: React.KeyboardEvent) => { | ||
if (e.which === 13 || e.which === 32) { | ||
setVisible(false) | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<span | ||
className={styles.highlightedText} | ||
onClick={openTooltip} | ||
onKeyDown={onOpenKeyDown} | ||
role="button" | ||
tabIndex={0} | ||
> | ||
{text} | ||
</span> | ||
{isVisible && ( | ||
<Portal> | ||
{/* eslint-disable jsx-a11y/no-static-element-interactions*/} | ||
{/* eslint-disable jsx-a11y/click-events-have-key-events */} | ||
<div className={styles.modalBackground} onClick={closeTooltip}> | ||
{/* eslint-enable jsx-a11y/no-static-element-interactions*/} | ||
{/* eslint-enable jsx-a11y/click-events-have-key-events */} | ||
<div className={styles.modalContent}> | ||
<div | ||
className={styles.closeContainer} | ||
onClick={closeTooltip} | ||
onKeyPress={onCloseKeyDown} | ||
role="button" | ||
tabIndex={0} | ||
> | ||
<div className={cn(styles.closeLine, styles.first)} /> | ||
<div className={cn(styles.closeLine, styles.second)} /> | ||
</div> | ||
<h5 className={styles.modalHeader}>{header}</h5> | ||
<ReactMarkdown className="markdown-body" source={description} /> | ||
</div> | ||
</div> | ||
</Portal> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default MobileView |
45 changes: 45 additions & 0 deletions
45
src/components/Documentation/Markdown/Tooltip/MobileView/styles.module.css
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,45 @@ | ||
.highlightedText { | ||
border-bottom: 1px black dotted; | ||
} | ||
|
||
.modalBackground { | ||
height: 100vh; | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
background-color: rgba(0, 0, 0, 0.4); | ||
} | ||
|
||
.closeContainer { | ||
float: right; | ||
margin: 2px 10px 0 0; | ||
} | ||
|
||
.closeLine { | ||
position: absolute; | ||
height: 23px; | ||
width: 2px; | ||
background-color: black; | ||
|
||
&.first { | ||
transform: rotate(-45deg); | ||
} | ||
|
||
&.second { | ||
transform: rotate(45deg); | ||
} | ||
} | ||
|
||
.modalContent { | ||
width: 80%; | ||
padding: 8px 10px; | ||
border: 1px solid var(--color-light-blue); | ||
border-radius: 3px; | ||
background-color: #fff; | ||
} | ||
|
||
.modalHeader { | ||
font-size: 1.3em; | ||
font-weight: bold; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/components/Documentation/Markdown/Tooltip/Portal/index.tsx
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,9 @@ | ||
import ReactDOM from 'react-dom' | ||
|
||
const Portal: React.SFC<{ children: React.ReactNode }> = ({ children }) => | ||
ReactDOM.createPortal( | ||
children, | ||
document.getElementById('portal') as HTMLElement | ||
) | ||
|
||
export default Portal |
Oops, something went wrong.