Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Add Tooltip TypeScript demos #15061

Merged
merged 2 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/src/pages/demos/tooltips/ControlledTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';

function ControlledTooltips() {
const [open, setOpen] = React.useState(false);

function handleTooltipClose() {
setOpen(false);
}

function handleTooltipOpen() {
setOpen(true);
}

return (
<Tooltip onClose={handleTooltipClose} onOpen={handleTooltipOpen} open={open} title="Add">
<Button>Controlled</Button>
</Tooltip>
);
}

export default ControlledTooltips;
198 changes: 198 additions & 0 deletions docs/src/pages/demos/tooltips/CustomizedTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import React from 'react';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';
import Typography from '@material-ui/core/Typography';

function arrowGenerator(color: string) {
return {
'&[x-placement*="bottom"] $arrow': {
top: 0,
left: 0,
marginTop: '-0.95em',
width: '3em',
height: '1em',
'&::before': {
borderWidth: '0 1em 1em 1em',
borderColor: `transparent transparent ${color} transparent`,
},
},
'&[x-placement*="top"] $arrow': {
bottom: 0,
left: 0,
marginBottom: '-0.95em',
width: '3em',
height: '1em',
'&::before': {
borderWidth: '1em 1em 0 1em',
borderColor: `${color} transparent transparent transparent`,
},
},
'&[x-placement*="right"] $arrow': {
left: 0,
marginLeft: '-0.95em',
height: '3em',
width: '1em',
'&::before': {
borderWidth: '1em 1em 1em 0',
borderColor: `transparent ${color} transparent transparent`,
},
},
'&[x-placement*="left"] $arrow': {
right: 0,
marginRight: '-0.95em',
height: '3em',
width: '1em',
'&::before': {
borderWidth: '1em 0 1em 1em',
borderColor: `transparent transparent transparent ${color}`,
},
},
};
}

const useStyles = makeStyles((theme: Theme) =>
createStyles({
button: {
margin: theme.spacing(1),
},
lightTooltip: {
backgroundColor: theme.palette.common.white,
color: 'rgba(0, 0, 0, 0.87)',
boxShadow: theme.shadows[1],
fontSize: 11,
},
arrowPopper: arrowGenerator(theme.palette.grey[700]),
arrow: {
position: 'absolute',
fontSize: 6,
width: '3em',
height: '3em',
'&::before': {
content: '""',
margin: 'auto',
display: 'block',
width: 0,
height: 0,
borderStyle: 'solid',
},
},
bootstrapPopper: arrowGenerator(theme.palette.common.black),
bootstrapTooltip: {
backgroundColor: theme.palette.common.black,
},
bootstrapPlacementLeft: {
margin: '0 8px',
},
bootstrapPlacementRight: {
margin: '0 8px',
},
bootstrapPlacementTop: {
margin: '8px 0',
},
bootstrapPlacementBottom: {
margin: '8px 0',
},
htmlPopper: arrowGenerator('#dadde9'),
htmlTooltip: {
backgroundColor: '#f5f5f9',
color: 'rgba(0, 0, 0, 0.87)',
maxWidth: 220,
fontSize: theme.typography.pxToRem(12),
border: '1px solid #dadde9',
'& b': {
fontWeight: theme.typography.fontWeightMedium,
},
},
}),
);

function CustomizedTooltips() {
const classes = useStyles();
const [arrowRef, setArrowRef] = React.useState<HTMLSpanElement | null>(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dudrie Try implementing the same feature with

Suggested change
const [arrowRef, setArrowRef] = React.useState<HTMLSpanElement | null>(null);
const arrowRef = React.useRef<HTMLSpanElement>(null);

You will probably need quite a bit of additional code to handle re-rendering.

The current implementation is a smart way to avoid creating ref callbacks and forceUpdate methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, useRef does not trigger a re-render if it's current changes. Using this hook would probably add more boilerplate code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly it's one of the reasons I don't like the ref suffix. When used right it doesn't add information but if used wrong it adds confusion. Simply using arrowNode would've been clearer and then it would also be obvious that setArrowNode should only be used in ref props for host or ref forwarding components.

But there's no reason to change this now. Discussions regarding this haven't been very helpful to either side anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to comment on the ref suffix situation last time. For me I don’t think the ref suffix is needed most of the time. I guess I would use it when passing something to the ref prop.


return (
<div>
<Tooltip title="Add" classes={{ tooltip: classes.lightTooltip }}>
<Button className={classes.button}>Light</Button>
</Tooltip>
<Tooltip
title={
<React.Fragment>
Add
<span className={classes.arrow} ref={setArrowRef} />
</React.Fragment>
}
classes={{ popper: classes.arrowPopper }}
PopperProps={{
popperOptions: {
modifiers: {
arrow: {
enabled: Boolean(arrowRef),
element: arrowRef,
},
},
},
}}
>
<Button className={classes.button}>Arrow</Button>
</Tooltip>
<Tooltip
title={
<React.Fragment>
Add
<span className={classes.arrow} ref={setArrowRef} />
</React.Fragment>
}
classes={{
tooltip: classes.bootstrapTooltip,
popper: classes.bootstrapPopper,
tooltipPlacementLeft: classes.bootstrapPlacementLeft,
tooltipPlacementRight: classes.bootstrapPlacementRight,
tooltipPlacementTop: classes.bootstrapPlacementTop,
tooltipPlacementBottom: classes.bootstrapPlacementBottom,
}}
PopperProps={{
popperOptions: {
modifiers: {
arrow: {
enabled: Boolean(arrowRef),
element: arrowRef,
},
},
},
}}
>
<Button className={classes.button}>Bootstrap</Button>
</Tooltip>
<Tooltip
classes={{
popper: classes.htmlPopper,
tooltip: classes.htmlTooltip,
}}
PopperProps={{
popperOptions: {
modifiers: {
arrow: {
enabled: Boolean(arrowRef),
element: arrowRef,
},
},
},
}}
title={
<React.Fragment>
<Typography color="inherit">Tooltip with HTML</Typography>
<em>{"And here's"}</em> <b>{'some'}</b> <u>{'amazing content'}</u>.{' '}
{"It's very engaging. Right?"}
<span className={classes.arrow} ref={setArrowRef} />
</React.Fragment>
}
>
<Button className={classes.button}>HTML</Button>
</Tooltip>
</div>
);
}

export default CustomizedTooltips;
13 changes: 13 additions & 0 deletions docs/src/pages/demos/tooltips/DelayTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';

function DelayTooltips() {
return (
<Tooltip title="Add" enterDelay={500} leaveDelay={200}>
<Button>[500ms, 200ms]</Button>
</Tooltip>
);
}

export default DelayTooltips;
15 changes: 15 additions & 0 deletions docs/src/pages/demos/tooltips/DisabledTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';

function DisabledTooltips() {
return (
<Tooltip title="You don't have permission to do this">
<span>
<Button disabled>A Disabled Button</Button>
</span>
</Tooltip>
);
}

export default DisabledTooltips;
33 changes: 33 additions & 0 deletions docs/src/pages/demos/tooltips/InteractiveTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyles, Theme, WithStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';

const styles = (theme: Theme) =>
createStyles({
button: {
margin: theme.spacing(1),
},
});

function InteractiveTooltips(props: WithStyles<typeof styles>) {
const { classes } = props;

return (
<div>
<Tooltip title="Add" interactive>
<Button className={classes.button}>Interactive</Button>
</Tooltip>
<Tooltip title="Add">
<Button className={classes.button}>Non Interactive</Button>
</Tooltip>
</div>
);
}

InteractiveTooltips.propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(InteractiveTooltips);
84 changes: 84 additions & 0 deletions docs/src/pages/demos/tooltips/PositionedTooltips.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles, createStyles, WithStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Button from '@material-ui/core/Button';
import Tooltip from '@material-ui/core/Tooltip';

const styles = createStyles({
root: {
width: 500,
},
});

function PositionedTooltips(props: WithStyles<typeof styles>) {
const { classes } = props;
return (
<div className={classes.root}>
<Grid container justify="center">
<Grid item>
<Tooltip title="Add" placement="top-start">
<Button>top-start</Button>
</Tooltip>
<Tooltip title="Add" placement="top">
<Button>top</Button>
</Tooltip>
<Tooltip title="Add" placement="top-end">
<Button>top-end</Button>
</Tooltip>
</Grid>
</Grid>
<Grid container justify="center">
<Grid item xs={6}>
<Tooltip title="Add" placement="left-start">
<Button>left-start</Button>
</Tooltip>
<br />
<Tooltip title="Add" placement="left">
<Button>left</Button>
</Tooltip>
<br />
<Tooltip title="Add" placement="left-end">
<Button>left-end</Button>
</Tooltip>
</Grid>
<Grid item container xs={6} alignItems="flex-end" direction="column">
<Grid item>
<Tooltip title="Add" placement="right-start">
<Button>right-start</Button>
</Tooltip>
</Grid>
<Grid item>
<Tooltip title="Add" placement="right">
<Button>right</Button>
</Tooltip>
</Grid>
<Grid item>
<Tooltip title="Add" placement="right-end">
<Button>right-end</Button>
</Tooltip>
</Grid>
</Grid>
</Grid>
<Grid container justify="center">
<Grid item>
<Tooltip title="Add" placement="bottom-start">
<Button>bottom-start</Button>
</Tooltip>
<Tooltip title="Add" placement="bottom">
<Button>bottom</Button>
</Tooltip>
<Tooltip title="Add" placement="bottom-end">
<Button>bottom-end</Button>
</Tooltip>
</Grid>
</Grid>
</div>
);
}

PositionedTooltips.propTypes = {
classes: PropTypes.object.isRequired,
} as any;

export default withStyles(styles)(PositionedTooltips);
Loading