import * as React from "react";
import * as System from "~/components/system";
import * as Constants from "~/common/constants";
import * as Events from "~/common/custom-events";
import Group from "~/components/system/Group";
import SystemPage from "~/components/system/SystemPage";
import ViewSourceLink from "~/components/system/ViewSourceLink";
import CodeBlock from "~/components/system/CodeBlock";
import { css } from "@emotion/react";
const STYLES_DEMO_TOOLTIP = {
display: "flex",
justifyContent: "center",
alignItems: "center",
};
const STYLES_TOOLTIP_BUBBLE = css`
display: inline-flex;
align-items: center;
background-color: ${Constants.system.black};
color: ${Constants.system.white};
opacity: 70%;
border-radius: 4px;
padding: 4px 8px;
height: 48px;
width: 160px;
font-size: 0.8em;
`;
export default class SystemPageTooltips extends React.Component {
state = {
horizontal: "center",
vertical: "above",
show: true,
};
_handleClick = (e, orientation, dir) => {
this.setState({ show: false, [orientation]: dir }, () => {
this.setState({ show: true }, () => {
Events.dispatchCustomEvent({
name: "show-tooltip",
detail: {
id: "orientation-tester-tooltip",
type: "body",
},
});
});
});
};
componentWillUnmount = () => {
Events.dispatchCustomEvent({
name: "remove-tooltip",
detail: {
id: "orientation-tester-tooltip",
type: "body",
},
});
};
render() {
let content = (
horizontal: "{this.state.horizontal}", vertical: "{this.state.vertical}"
);
return (
Tooltips
The Tooltip component is used to provide the user with more information in a message that
appears when they interact with an element.
Imports
Import the GlobalTooltip, TooltipWrapper, and optionally the TooltipAnchor Components.
{`import * as React from "react";
import { GlobalTooltip, TooltipWrapper, TooltipAnchor } from "slate-react-system";`}
Tooltip
Declare the GlobalTooltip at the root level of your
document (e.g. in index.js or App.js) so it is accessible throughout and will not get
buried in the DOM tree.
{`class App extends React.Component {
render() {
return (
);
}
}`}
Then, wrap your desired anchor with a TooltipWrapper.
The wrapper's id should match the id in the dispatchCustomEvent call. This id must be
unique for each tooltip.
The tooltip component, passed in as content to{" "}
TooltipWrapper, will be displayed when a
dispatchCustomEvent is called with its id.
{`class ExampleOne extends React.Component {
_handleMouseEnter = (e) => {
dispatchCustomEvent({
name: "show-tooltip",
detail: {
id: "unique-tooltip-id",
},
});
};
_handleMouseLeave = (e) => {
dispatchCustomEvent({
name: "hide-tooltip",
detail: {
id: "unique-tooltip-id",
},
});
};
render() {
let content = (
{this.props.tooltip}
);
return (
)
}
}`}
Tooltip Anchor
For a pre-styled tooltip that accepts a string and handles dispatchCustomEvent and styling
for you, use the TooltipAnchor component. Be sure to
give it a unique id.
{`class ExampleTwo extends React.Component {
render() {
return ;
}
}`}
Setting an Orientation
You can set a tooltip to appear in a set orientation using the{" "}
horizontal and{" "}
vertical props. These can be applied to both the{" "}
TooltipWrapper and the{" "}
TooltipAnchor components.
Horizontal
{["far-left", "left", "center", "right", "far-right"].map((dir, i) => (
{
this._handleClick(e, "horizontal", dir);
}}
>
{dir}
))}
{this.state.show ? (
) : null}
Vertical
{["above", "up", "center", "down", "below"].map((dir, i) => (
{
this._handleClick(e, "vertical", dir);
}}
>
{dir}
))}
{`class ExampleThree extends React.Component {
render() {
return (
);
}
}`}
Accepted React Properties
id,
b: "string",
c: "null",
d: "Unique id to identify the tooltip.",
},
{
id: 2,
a: "tooltip",
b: "string",
c: "null",
d: "Output text on the tooltip bubble.",
},
{
id: 3,
a: "height",
b: "number",
c: "24px",
d: "Height of the tooltip anchor icon.",
},
{
id: 4,
a: "style",
b: "Object",
c: "null",
d: "Style applied to the tooltip bubble.",
},
{
id: 5,
a: "anchorStyle",
b: "Object",
c: "null",
d: "Style applied to the tooltip anchor.",
},
{
id: 6,
a: "children",
b: "Object",
c: "null",
d:
"Will be rendered instead of the default question mark SVG as the tooltip anchor.",
},
{
id: 7,
a: "horizontal",
b: "string",
c: "center",
d:
"Horizontal positioning of the tooltip relative to the anchor (far-left, left, center, right, far-right)",
},
{
id: 8,
a: "vertical",
b: "string",
c: "above",
d:
"Vertical positioning of the tooltip relative to the anchor (above, up, center, down, below)",
},
],
}}
/>
id,
b: "string",
c: "null",
d: "Unique id to identify the tooltip.",
},
{
id: 2,
a: "content",
b: "Component",
c: "null",
d: "Component rendered as the tooltip bubble.",
},
{
id: 3,
a: "horizontal",
b: "string",
c: "center",
d:
"Horizontal positioning of the tooltip relative to the anchor (far-left, left, center, right, far-right)",
},
{
id: 4,
a: "vertical",
b: "string",
c: "above",
d:
"Vertical positioning of the tooltip relative to the anchor (above, up, center, down, below)",
},
{
id: 5,
a: "children",
b: "Component",
c: "null",
d: "The tooltip anchor",
},
],
}}
/>
);
}
}