This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from MrJarv1s/tooltip
Tooltip
- Loading branch information
Showing
8 changed files
with
153 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Tabler React Style Guide</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,500,500i,600,600i,700,700i&subset=latin-ext"></head><body><div id="rsg-root"></div><script src="build/bundle.495767ab.js"></script></body></html> | ||
<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Tabler React Style Guide</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,500,500i,600,600i,700,700i&subset=latin-ext"></head><body><div id="rsg-root"></div><script src="build/bundle.3b1ce0e9.js"></script></body></html> |
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,9 @@ | ||
/* Tooltip-specific Stylesheet */ | ||
|
||
.tbr-arrow-vertical { | ||
left: calc(50% - 0.4rem); | ||
} | ||
|
||
.tbr-arrow-horizontal { | ||
top: calc(50% - 0.4rem); | ||
} |
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,7 @@ | ||
### Basic Tooltip | ||
|
||
```jsx | ||
<Tooltip content="Tooltip" placement="top"> | ||
<Tag>Hover Me!</Tag> | ||
</Tooltip> | ||
``` |
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,105 @@ | ||
// @flow | ||
|
||
import * as React from "react"; | ||
import cn from "classnames"; | ||
import { Manager, Placement, Reference, Popper } from "react-popper"; | ||
import type { PopperChildrenProps, ReferenceChildrenProps } from "react-popper"; | ||
import styles from "./Tooltip.css"; | ||
|
||
type Props = {| | ||
/** | ||
* The reference element which the Tooltip will be based on. | ||
*/ | ||
+children?: React.Element<any>, | ||
/** | ||
* Any additional classNames for the Tooltip. | ||
*/ | ||
+className?: string, | ||
/** | ||
* This is the text content of the Tooltip. | ||
*/ | ||
+content: string, | ||
/** | ||
* This is the placement of the Tooltip (top, bottom, left, right). | ||
*/ | ||
+placement?: Placement, | ||
+type?: "link", | ||
|}; | ||
|
||
type State = { | ||
isShown: boolean, | ||
}; | ||
|
||
class Tooltip extends React.Component<Props, State> { | ||
state = { isShown: false }; | ||
|
||
_handleTriggerOnMouseEnter = (e: SyntheticMouseEvent<HTMLElement>) => { | ||
e.preventDefault(); | ||
this.setState({ isShown: true }); | ||
}; | ||
|
||
_handleTriggerOnMouseLeave = (e: SyntheticMouseEvent<HTMLElement>) => { | ||
e.preventDefault(); | ||
this.setState({ isShown: false }); | ||
}; | ||
|
||
render(): React.Node { | ||
const { className, children, placement, content } = this.props; | ||
|
||
const classes = cn( | ||
"tooltip", | ||
placement && "bs-tooltip-" + placement, | ||
{ | ||
show: this.state.isShown, | ||
}, | ||
className | ||
); | ||
|
||
const arrowClasses = cn( | ||
"arrow", | ||
placement === "top" || placement === "bottom" | ||
? styles["tbr-arrow-vertical"] | ||
: styles["tbr-arrow-horizontal"] | ||
); | ||
|
||
return ( | ||
<Manager> | ||
<Reference> | ||
{({ ref }: ReferenceChildrenProps) => | ||
typeof children !== "undefined" && | ||
(this.props.type | ||
? React.cloneElement(children, { | ||
ref: ref, | ||
onMouseEnter: this._handleTriggerOnMouseEnter, | ||
onMouseLeave: this._handleTriggerOnMouseLeave, | ||
}) | ||
: React.cloneElement(children, { | ||
rootRef: ref, | ||
onMouseEnter: this._handleTriggerOnMouseEnter, | ||
onMouseLeave: this._handleTriggerOnMouseLeave, | ||
})) | ||
} | ||
</Reference> | ||
{this.state.isShown && ( | ||
<Popper placement={placement} eventsEnabled={true}> | ||
{({ ref, style, placement }: PopperChildrenProps) => { | ||
return ( | ||
<div | ||
className={classes} | ||
data-placement={placement} | ||
style={style} | ||
ref={ref} | ||
> | ||
<div className={arrowClasses} /> | ||
<div className="tooltip-inner">{content}</div> | ||
</div> | ||
); | ||
}} | ||
</Popper> | ||
)} | ||
</Manager> | ||
); | ||
} | ||
} | ||
|
||
export default Tooltip; |
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,5 @@ | ||
// @flow | ||
|
||
import Tooltip from "./Tooltip.react"; | ||
|
||
export { Tooltip as default }; |
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