-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(highlight): Render promoter tooltip (#557)
- Loading branch information
Mingze
authored
Aug 27, 2020
1 parent
e1081b0
commit b92f8bb
Showing
18 changed files
with
383 additions
and
109 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,91 @@ | ||
@mixin ba-PopupArrow($size, $top-bg, $right-bg, $bottom-bg, $left-bg, $border-color) { | ||
// Top-facing arrows | ||
&[data-popper-placement*='bottom'] { | ||
.ba-Popup-arrow { | ||
top: -#{$size - 1}; | ||
|
||
&::before, | ||
&::after { | ||
transform: rotate(180deg) translateX(50%); | ||
} | ||
|
||
&::after { | ||
top: 1px; | ||
border-top-color: $top-bg; | ||
} | ||
} | ||
} | ||
|
||
// Right-facing arrows | ||
&[data-popper-placement*='left'] { | ||
.ba-Popup-arrow { | ||
right: 0; | ||
|
||
&::before, | ||
&::after { | ||
transform: rotate(-90deg) translateY(-100%); | ||
transform-origin: center top; | ||
} | ||
|
||
&::after { | ||
left: -1px; | ||
border-top-color: $right-bg; | ||
} | ||
} | ||
} | ||
|
||
// Left-facing arrows | ||
&[data-popper-placement*='right'] { | ||
.ba-Popup-arrow { | ||
left: -#{$size - 1}; | ||
|
||
&::before, | ||
&::after { | ||
transform: rotate(90deg); | ||
transform-origin: center top; | ||
} | ||
|
||
&::after { | ||
left: 1px; | ||
border-top-color: $left-bg; | ||
} | ||
} | ||
} | ||
|
||
// Bottom-facing arrows | ||
&[data-popper-placement*='top'] { | ||
.ba-Popup-arrow { | ||
top: auto; | ||
bottom: 0; | ||
|
||
&::before, | ||
&::after { | ||
transform: rotate(0) translateX(-50%); | ||
} | ||
|
||
&::after { | ||
top: -1px; | ||
border-top-color: $bottom-bg; | ||
} | ||
} | ||
} | ||
|
||
.ba-Popup-arrow { | ||
&::before, | ||
&::after { | ||
position: absolute; | ||
display: block; | ||
border-right: $size solid transparent; | ||
border-left: $size solid transparent; | ||
content: ''; | ||
} | ||
|
||
&::before { | ||
border-top: $size solid $border-color; | ||
} | ||
|
||
&::after { | ||
border-top: $size solid; | ||
} | ||
} | ||
} |
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,39 @@ | ||
@import '~box-ui-elements/es/styles/variables'; | ||
@import './PopupArrow'; | ||
|
||
.ba { | ||
$popup-content-bg: $bdl-gray; | ||
|
||
.ba-PopupHighlight { | ||
@include ba-PopupArrow(4px, $popup-content-bg, $popup-content-bg, $popup-content-bg, $popup-content-bg, $popup-content-bg); | ||
|
||
.ba-Popup-content { | ||
background-color: $popup-content-bg; | ||
border: none; | ||
border-radius: $bdl-border-radius-size; | ||
box-shadow: none; | ||
} | ||
} | ||
|
||
.ba-PopupHighlight-button { | ||
@include common-typography; | ||
|
||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
padding: 5px 10px; | ||
color: $white; | ||
background: transparent; | ||
border: none; | ||
border-radius: $bdl-border-radius-size; | ||
cursor: pointer; | ||
} | ||
|
||
.ba-PopupHighlight-icon { | ||
margin-right: 8px; | ||
|
||
path { | ||
fill: $white; | ||
} | ||
} | ||
} |
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,102 @@ | ||
import React from 'react'; | ||
import IconHighlightTextAnnotation from 'box-ui-elements/es/icon/fill/AnnotationsHighlight16'; | ||
import noop from 'lodash/noop'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import messages from './messages'; | ||
import PopupBase from './PopupBase'; | ||
import { Options } from './Popper'; | ||
import { Shape } from '../../@types/model'; | ||
import './PopupHighlight.scss'; | ||
|
||
export type Props = { | ||
onClick?: (event: React.MouseEvent) => void; | ||
shape: Shape; | ||
}; | ||
|
||
const options: Partial<Options> = { | ||
modifiers: [ | ||
{ | ||
name: 'arrow', | ||
options: { | ||
element: '.ba-Popup-arrow', | ||
}, | ||
}, | ||
{ | ||
name: 'eventListeners', | ||
options: { | ||
scroll: false, | ||
}, | ||
}, | ||
{ | ||
name: 'offset', | ||
options: { | ||
offset: [0, 8], | ||
}, | ||
}, | ||
{ | ||
name: 'preventOverflow', | ||
options: { | ||
padding: 5, | ||
}, | ||
}, | ||
], | ||
placement: 'bottom', | ||
}; | ||
|
||
export default function PopupHighlight({ onClick = noop, shape }: Props): JSX.Element { | ||
const buttonRef = React.useRef<HTMLButtonElement>(null); | ||
const { height, width, x, y } = shape; | ||
|
||
const reference = { | ||
getBoundingClientRect: () => ({ | ||
bottom: y + height, | ||
height, | ||
left: x, | ||
right: x + width, | ||
top: y, | ||
width, | ||
}), | ||
}; | ||
|
||
const handleEvent = (event: Event): void => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
}; | ||
|
||
const handleClick = (event: React.MouseEvent): void => { | ||
onClick(event); | ||
}; | ||
|
||
// Prevent events from propagating to upper elements. The upper elements' | ||
// mouse handlers are attached to real dom, so the belows have to attach to real dom too | ||
React.useEffect(() => { | ||
const { current: buttonEl } = buttonRef; | ||
|
||
if (buttonEl) { | ||
buttonEl.addEventListener('mousedown', handleEvent); | ||
buttonEl.addEventListener('mouseup', handleEvent); | ||
} | ||
|
||
return () => { | ||
if (buttonEl) { | ||
buttonEl.removeEventListener('mousedown', handleEvent); | ||
buttonEl.removeEventListener('mouseup', handleEvent); | ||
} | ||
}; | ||
}, [buttonRef]); | ||
|
||
return ( | ||
<PopupBase className="ba-PopupHighlight" options={options} reference={reference}> | ||
<button | ||
ref={buttonRef} | ||
className="ba-PopupHighlight-button" | ||
data-testid="ba-PopupHighlight-button" | ||
onClick={handleClick} | ||
type="button" | ||
> | ||
<IconHighlightTextAnnotation className="ba-PopupHighlight-icon" /> | ||
<FormattedMessage {...messages.popupHighlightPromoter} /> | ||
</button> | ||
</PopupBase> | ||
); | ||
} |
Oops, something went wrong.