1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-06 06:07:33 +02:00

feat: Tooltip

This commit is contained in:
Phuoc Nguyen
2022-09-20 18:29:40 +07:00
parent 603336d667
commit f5cc51bb9d
7 changed files with 161 additions and 192 deletions

View File

@@ -0,0 +1,4 @@
<div class="tooltip">
<div class="tooltip__content">{% lines "hor", 5 %}</div>
<div class="tooltip__arrow"></div>
</div>

View File

@@ -77,6 +77,7 @@ eleventyExcludeFromCollections: true
{% pattern "Progress bar" %}{% include "patterns/progress-bar.njk" %}{% endpattern %}
{% pattern "Radial progress bar" %}{% include "patterns/radial-progress-bar.njk" %}{% endpattern %}
{% pattern "Resizable element" %}{% include "patterns/resizable-element.njk" %}{% endpattern %}
{% pattern "Tooltip" %}{% include "patterns/tooltip.njk" %}{% endpattern %}
</div>
</div>

89
contents/tooltip.md Normal file
View File

@@ -0,0 +1,89 @@
---
layout: layouts/post.njk
title: Tooltip
description: Create a tooltip with CSS
keywords: css tooltip
---
## HTML
```html
<div class="tooltip">
<!-- The tooltip content -->
<div class="tooltip__content">
...
</div>
<!-- The tooltip arrow -->
<div class="tooltip__arrow"></div>
<!-- The trigger element -->
...
</div>
```
## CSS
```css
.tooltip {
/* Used to position the arrow */
position: relative;
}
/* Show the arrow and content and restore pointer events when hovering the trigger element */
.tooltip:hover .tooltip__arrow,
.tooltip:hover .tooltip__content {
opacity: 1;
pointer-events: initial;
}
.tooltip__arrow {
/* Invisible by default */
opacity: 0;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Border */
border: 0.5rem solid transparent;
border-top-color: #111827;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, 8px);
/* Zero size */
height: 0;
width: 0;
/* Displayed on top of other element */
z-index: 10;
}
.tooltip__content {
/* Invisible by default */
opacity: 0;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Background color, must be the same as the border color of arrow */
background-color: #111827;
border-radius: 0.25rem;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, -8px);
/* Displayed on top of other element */
z-index: 10;
}
```
{% demo %}
{% include "patterns/tooltip.njk" %}
{% enddemo %}

View File

@@ -1,136 +0,0 @@
import * as React from 'react';
import Head from 'next/head';
import { Spacer } from '@1milligram/design';
import { RelatedPatterns } from '../../components/RelatedPatterns';
import { Pattern } from '../../constants/Pattern';
import { PatternLayout } from '../../layouts/PatternLayout';
import Block from '../../placeholders/Block';
import BrowserFrame from '../../placeholders/BrowserFrame';
import Rectangle from '../../placeholders/Rectangle';
const Details: React.FC<{}> = () => {
return (
<PatternLayout pattern={Pattern.Tooltip}>
<Head>
<meta name="description" content="Create a tooltip with CSS" />
<meta name="og:description" content="Create a tooltip with CSS" />
<meta name="twitter:description" content="Create a tooltip with CSS" />
<meta name="keywords" content="css tooltip" />
</Head>
<div style={{ lineHeight: 1.5, marginBottom: '16px' }}>
Move the mouser over the main element to see the tooltip.
</div>
<BrowserFrame
html={`
<div class="container">
<!-- The tooltip content -->
<div class="container__content">
...
</div>
<!-- The tooltip arrow -->
<div class="container__arrow" />
<!-- The trigger element -->
...
</div>
`}
css={`
.container {
/* Used to position the arrow */
position: relative;
}
/* Show the arrow and content and restore pointer events when hovering the trigger element */
.container:hover .container__arrow,
.container:hover .container__content {
opacity: 1;
pointer-events: initial;
}
.container__arrow {
/* Invisible by default */
opacity: 0;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Border */
border: 8px solid transparent;
border-top-color: #00439e;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, 8px);
/* Zero size */
height: 0;
width: 0;
/* Displayed on top of other element */
z-index: 10;
}
.container__content {
/* Invisible by default */
opacity: 0;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Background color, must be the same as the border color of arrow */
background-color: #00439e;
border-radius: 2px;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, -8px);
/* Displayed on top of other element */
z-index: 10;
}
`}
>
<div
style={{
alignItems: 'center',
display: 'flex',
flexDirection: 'column',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div
className="p-tooltip"
style={{
marginBottom: '16px',
width: '150px',
}}
>
<div
className="p-tooltip-content"
style={{
padding: '8px',
width: '200px',
}}
>
<Block backgroundColor="#fff" justify="center" numberOfBlocks={5} />
</div>
<div className="p-tooltip-arrow" />
<Rectangle height={32} />
</div>
</div>
</BrowserFrame>
<Spacer size="extraLarge" />
<RelatedPatterns patterns={[Pattern.PopoverArrow]} />
</PatternLayout>
);
};
export default Details;

View File

@@ -1,56 +0,0 @@
import * as React from 'react';
import Frame from '../../placeholders/Frame';
import Rectangle from '../../placeholders/Rectangle';
const Cover: React.FC<{}> = () => {
return (
<Frame>
<div
style={{
alignItems: 'center',
display: 'flex',
flexDirection: 'column',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div
style={{
position: 'relative',
width: '60%',
}}
>
<div
style={{
border: '4px solid transparent',
borderTopColor: '#00439e',
bottom: '100%',
height: 0,
left: '50%',
position: 'absolute',
transform: 'translate(-50%, 4px)',
width: 0,
}}
/>
<div
style={{
backgroundColor: '#00439e',
borderRadius: '2px',
bottom: '100%',
height: '24px',
left: '50%',
position: 'absolute',
transform: 'translate(-50%, -4px)',
width: '80px',
}}
/>
<Rectangle height={16} />
</div>
</div>
</Frame>
);
};
export default Cover;

View File

@@ -63,6 +63,7 @@
@import './patterns/teardrop';
@import './patterns/three-dimensions-card';
@import './patterns/timeline';
@import './patterns/tooltip';
@import './patterns/tree-diagram';
@import './patterns/triangle-buttons';
@import './patterns/video-background';

View File

@@ -0,0 +1,66 @@
.tooltip {
/* Used to position the arrow */
position: relative;
/* Demo */
width: 8rem;
height: 2rem;
border-radius: 0.25rem;
background: #d1d5db;
}
/* Show the arrow and content and restore pointer events when hovering the trigger element */
.tooltip:hover .tooltip__arrow,
.tooltip:hover .tooltip__content {
opacity: 1;
pointer-events: initial;
}
.tooltip__arrow {
/* Invisible by default */
opacity: 1;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Border */
border: 0.5rem solid transparent;
border-top-color: #111827;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, 8px);
/* Zero size */
height: 0;
width: 0;
/* Displayed on top of other element */
z-index: 10;
}
.tooltip__content {
/* Invisible by default */
opacity: 1;
/* To prevent accidental interactions with other elements */
pointer-events: none;
/* Background color, must be the same as the border color of arrow */
background-color: #111827;
border-radius: 0.25rem;
/* Position */
bottom: 100%;
left: 50%;
position: absolute;
transform: translate(-50%, -8px);
/* Displayed on top of other element */
z-index: 10;
/* Demo */
width: 6rem;
}