Skip to content

Commit 3a35a72

Browse files
committed
fix(tooltip): small tweaks to the tooltip state
1 parent dd22ec2 commit 3a35a72

File tree

11 files changed

+70
-161
lines changed

11 files changed

+70
-161
lines changed

.changeset/tasty-pugs-decide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik-ui/headless': minor
3+
---
4+
5+
Implement a beta version of the Tooltip component

apps/website/src/routes/docs/headless/tooltip/examples/animation.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { component$ } from '@builder.io/qwik';
22
import { Tooltip } from '@qwik-ui/headless';
33

4+
import '../snippets/animation.css';
5+
46
export default component$(() => {
57
return (
68
<Tooltip.Root gutter={4} flip>

apps/website/src/routes/docs/headless/tooltip/examples/arrow-styling.tsx

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
1-
import { component$, useStyles$ } from '@builder.io/qwik';
1+
import { component$ } from '@builder.io/qwik';
22
import { Tooltip } from '@qwik-ui/headless';
33

4-
export default component$(() => {
5-
useStyles$(`
6-
.tooltip-panel {
7-
background-color: #222;
8-
color: #fff;
9-
padding: 15px;
10-
border-radius: 8px;
11-
position: relative;
12-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
13-
transition: opacity 0.3s ease;
14-
opacity: 0;
15-
}
16-
17-
.tooltip-panel[data-state="open"] {
18-
opacity: 1;
19-
}
20-
21-
.tooltip-arrow {
22-
position: absolute;
23-
width: 20px;
24-
height: 10px;
25-
overflow: hidden;
26-
}
27-
28-
.tooltip-arrow::before {
29-
content: '';
30-
position: absolute;
31-
width: 10px;
32-
height: 10px;
33-
background-color: #222;
34-
top: -5px;
35-
left: calc(50% - 5px);
36-
transform: rotate(45deg);
37-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
38-
transition: top 0.3s ease;
39-
}
40-
41-
.tooltip-panel[data-state="open"] .tooltip-arrow::before {
42-
top: -8px;
43-
}
44-
`);
4+
import '../snippets/arrow-styling.css';
455

6+
export default component$(() => {
467
return (
478
<Tooltip.Root gutter={4} flip>
489
<Tooltip.Trigger>Hover or Focus me</Tooltip.Trigger>
49-
<Tooltip.Panel aria-label="Tooltip content" class="tooltip-panel">
50-
<Tooltip.Arrow class="tooltip-arrow" width={20} height={10} />
10+
<Tooltip.Panel aria-label="Tooltip content" class="tooltip-arrow-styled-panel">
11+
<Tooltip.Arrow class="tooltip-arrow-styled-arrow" width={20} height={10} />
5112
<div>
5213
<h3 style="margin: 0 0 10px;">Tooltip Title</h3>
5314
<p style="margin: 0;">This tooltip has a snazzy styled arrow!</p>

apps/website/src/routes/docs/headless/tooltip/examples/transition.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { component$ } from '@builder.io/qwik';
22
import { Tooltip } from '@qwik-ui/headless';
33

4+
import '../snippets/transition.css';
5+
46
export default component$(() => {
57
return (
68
<Tooltip.Root gutter={4} flip>

apps/website/src/routes/docs/headless/tooltip/index.mdx

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -109,50 +109,20 @@ Tooltips are useful for displaying contextual information or additional details
109109
### Styling open tooltips
110110

111111
```tsx
112-
.tooltip-panel[data-state="open"] {
112+
.tooltip-panel[data-open] {
113113
background: lightblue;
114114
}
115115
```
116116

117-
Use the data-state attribute on the `<Tooltip.Panel>` component to specifically style the tooltip when it's open.
117+
Use the `data-open` and `data-closed` attributes on the `<Tooltip.Panel>` component to specifically style the tooltip when it's open.
118118

119119
### Cross-browser Animations
120120

121121
Entry and exit animations have often been a frustrating experience in web development, especially trying to animate between `display: none`, a discrete property.
122122

123-
Luckily, the Qwik UI team has done an excellent job of managing animations on polyfill browsers for you using the `data-state` attributes.
123+
Luckily, the Qwik UI team has done an excellent job of managing animations on polyfill browsers for you using the `data-*` attributes.
124124

125-
```css
126-
.tooltip-animation {
127-
transform: scale(0);
128-
}
129-
130-
.tooltip-animation[data-state='open'] {
131-
animation: tooltip-grow 0.5s ease-in-out forwards;
132-
}
133-
134-
.tooltip-animation[data-state='closing'] {
135-
animation: tooltip-shrink 0.4s ease-in-out forwards;
136-
}
137-
138-
@keyframes tooltip-shrink {
139-
from {
140-
transform: scale(1);
141-
}
142-
to {
143-
transform: scale(0);
144-
}
145-
}
146-
147-
@keyframes tooltip-grow {
148-
from {
149-
transform: scale(0);
150-
}
151-
to {
152-
transform: scale(1);
153-
}
154-
}
155-
```
125+
<CodeSnippet name="animation.css" />
156126

157127
## Tooltip Behavior
158128

@@ -206,42 +176,25 @@ If Tailwind is the framework of choice, then styles can be added using the [arbi
206176

207177
To use an animation, add the following CSS classes to the component.
208178

209-
- The `data-state="open"` attribute determines the animation that happens when it is first opened.
179+
- The `data-open` attribute determines the animation that happens when it is first opened.
210180

211-
- The `data-state="closing"` class determines what class is added when the tooltip is **closed**.
181+
- The `data-closed` class determines what class is added when the tooltip is **closed**.
212182

213183
Here's the CSS imported from the example:
214184

215185
<CodeSnippet name="animation.css" />
216186

217187
### Transition declarations
218188

219-
Transitions use the same classes for entry and exit animations. Those being `data-state="opening"` and `data-state="closing"`. They are explained more in the `Caveats` section.
189+
Transitions use the same classes for entry and exit animations. Those being `data-open` and `data-closed`. They are explained more in the `Caveats` section.
220190

221191
<Showcase name="transition" />
222192

223193
> The [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) is another native solution that aims to solve animating between states. Support is currently in around **~71%** of browsers.
224194
225195
CSS from the example:
226196

227-
```css
228-
.tooltip-transition {
229-
opacity: 0;
230-
transition:
231-
opacity 0.5s,
232-
display 0.5s,
233-
overlay 0.5s;
234-
transition-behavior: allow-discrete;
235-
}
236-
237-
.tooltip-transition[data-state='open'] {
238-
opacity: 1;
239-
}
240-
241-
.tooltip-transition[data-state='closing'] {
242-
opacity: 0;
243-
}
244-
```
197+
<CodeSnippet name="transition.css" />
245198

246199
## Additional References
247200

@@ -279,23 +232,23 @@ To read more about tooltips you can check it out on:
279232
description: 'The space between the trigger element and the tooltip.',
280233
},
281234
{
282-
name: 'data-state="closing"',
235+
name: 'data-closing"',
283236
type: 'selector',
284237
description: 'Style the element when the tooltip is closing. This occurs when the popover has a delay set.',
285238
},
286239
{
287-
name: 'data-state="closed"',
240+
name: 'data-closed',
288241
type: 'selector',
289242
description: 'Style the element when the tooltip is closed.',
290243
},
291244

292245
{
293-
name: 'data-state="opening"',
246+
name: 'data-opening',
294247
type: 'selector',
295248
description: 'Style the element when the tooltip is in the process of opening. This occurs when the popover has a delay set.',
296249
},
297250
{
298-
name: 'data-state="open"',
251+
name: 'data-open',
299252
type: 'selector',
300253
description: 'Style the element when the tooltip is open.',
301254
},

apps/website/src/routes/docs/headless/tooltip/snippets/animation.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
transform: scale(0);
33
}
44

5-
.tooltip-animation[data-state='open'] {
5+
.tooltip-animation[data-open] {
66
animation: tooltip-grow 0.5s ease-in-out forwards;
77
}
88

9-
.tooltip-animation[data-state='closing'] {
9+
.tooltip-animation[data-closing] {
1010
animation: tooltip-shrink 0.4s ease-in-out forwards;
1111
}
1212

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.tooltip-arrow-styled-panel {
2+
background-color: #222;
3+
color: #fff;
4+
padding: 15px;
5+
border-radius: 8px;
6+
position: relative;
7+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
8+
transition: opacity 0.3s ease;
9+
opacity: 0;
10+
}
11+
12+
.tooltip-arrow-styled-panel[data-open] {
13+
opacity: 1;
14+
}
15+
16+
.tooltip-arrow-styled-arrow {
17+
position: absolute;
18+
width: 20px;
19+
height: 10px;
20+
overflow: hidden;
21+
}
22+
23+
.tooltip-arrow-styled-arrow::before {
24+
content: '';
25+
position: absolute;
26+
width: 10px;
27+
height: 10px;
28+
background-color: #222;
29+
top: -5px;
30+
left: calc(50% - 5px);
31+
transform: rotate(45deg);
32+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
33+
transition: top 0.3s ease;
34+
}
35+
36+
.tooltip-arrow-styled-panel[data-open] .tooltip-arrow-styled-arrow::before {
37+
top: -8px;
38+
}

apps/website/src/routes/docs/headless/tooltip/snippets/tooltip.css

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,3 @@
2121
0 4px 6px -1px rgba(0, 0, 0, 0.1),
2222
0 2px 4px -1px rgba(0, 0, 0, 0.06);
2323
}
24-
25-
.tooltip-animation {
26-
transform: scale(0);
27-
}
28-
29-
.tooltip-animation[data-state='open'] {
30-
animation: tooltip-grow 0.5s ease-in-out forwards;
31-
}
32-
33-
.tooltip-animation[data-state='closing'] {
34-
animation: tooltip-shrink 0.4s ease-in-out forwards;
35-
}
36-
37-
@keyframes tooltip-shrink {
38-
from {
39-
transform: scale(1);
40-
}
41-
to {
42-
transform: scale(0);
43-
}
44-
}
45-
46-
@keyframes tooltip-grow {
47-
from {
48-
transform: scale(0);
49-
}
50-
to {
51-
transform: scale(1);
52-
}
53-
}
54-
55-
.tooltip-transition {
56-
opacity: 0;
57-
transition:
58-
opacity 0.5s,
59-
display 0.5s,
60-
overlay 0.5s;
61-
transition-behavior: allow-discrete;
62-
}
63-
64-
.tooltip-transition[data-state='open'] {
65-
opacity: 1;
66-
}
67-
68-
.tooltip-transition[data-state='closing'] {
69-
opacity: 0;
70-
}

apps/website/src/routes/docs/headless/tooltip/snippets/transition.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
transition-behavior: allow-discrete;
88
}
99

10-
.tooltip-transition[data-state='opening'] {
10+
.tooltip-transition[data-open] {
1111
opacity: 1;
1212
}
1313

14-
.tooltip-transition[data-state='closing'] {
14+
.tooltip-transition[data-closed] {
1515
opacity: 0;
1616
}

packages/kit-headless/src/components/tooltip/tooltip-panel.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ export const HTooltipPanel = component$((props: HTooltipPanelProps) => {
1111
const context = useContext(TooltipContextId);
1212

1313
return (
14-
<HPopoverPanel
15-
{...props}
16-
role="tooltip"
17-
id={context.localId}
18-
data-state={context.state.value}
19-
>
14+
<HPopoverPanel {...props} role="tooltip" id={context.localId}>
2015
<Slot />
2116
</HPopoverPanel>
2217
);

packages/kit-headless/src/components/tooltip/tooltip-root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const HTooltipRoot = component$((props: TooltipProps) => {
8181
return (
8282
<HPopoverRoot
8383
manual
84-
hover={true}
84+
hover
8585
bind:anchor={triggerRef}
8686
floating={placement}
8787
id={localId}

0 commit comments

Comments
 (0)