-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -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; |
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,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); | ||
|
||
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; |
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,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; |
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,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; |
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,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); |
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,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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
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.There was a problem hiding this comment.
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'scurrent
changes. Using this hook would probably add more boilerplate code.There was a problem hiding this comment.
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 usingarrowNode
would've been clearer and then it would also be obvious thatsetArrowNode
should only be used inref
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.
There was a problem hiding this comment.
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 theref
prop.