Skip to content

Commit

Permalink
refactor(Tooltip): use CSS instead of JS for tooltip display
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeyffrey committed Jun 13, 2024
1 parent a26437a commit 4e70a8f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 55 deletions.
10 changes: 1 addition & 9 deletions addon/components/pix-tooltip.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<div
class="pix-tooltip"
{{on-escape-action this.hideTooltip}}
{{on "mouseover" this.showTooltip}}
{{on "mouseleave" this.hideTooltipOnMouseOut}}
{{on "focusin" this.showTooltip}}
{{on "focusout" this.hideTooltip}}
...attributes
>
<div class="pix-tooltip" ...attributes>
{{#if (has-block "triggerElement")}}
<span class="pix-tooltip__trigger-element">
{{yield to="triggerElement"}}
Expand Down
22 changes: 0 additions & 22 deletions addon/components/pix-tooltip.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export default class PixTooltip extends Component {
Expand All @@ -22,25 +21,4 @@ export default class PixTooltip extends Component {
get display() {
return typeof this.args.hide === 'undefined' || !this.args.hide;
}

@action
showTooltip() {
this.isVisible = true;
}

@action
hideTooltip() {
this.isVisible = false;
}

@action
hideTooltipOnMouseOut(event) {
const isFocused = event.target.contains(document.activeElement);

if (isFocused) {
return;
}

this.hideTooltip(event);
}
}
26 changes: 21 additions & 5 deletions addon/styles/_pix-tooltip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
display: block;
width: 100%;
}

&__trigger-element:hover + .pix-tooltip__content {
display: block;
opacity: 1;
}
}

@supports selector(:has(*)) {
.pix-tooltip:has(.pix-tooltip__trigger-element :focus-visible) {
.pix-tooltip__content {
display: block;
opacity: 1;
}
}
}

@supports not selector(:has(*)) {
.pix-tooltip__trigger-element:focus-within + .pix-tooltip__content {
display: block;
opacity: 1;
}
}

.pix-tooltip__content {
Expand All @@ -27,11 +48,6 @@
transition: opacity 0.3s;
pointer-events: none;

&--visible {
display: block;
opacity: 1;
}

&--inline {
white-space: nowrap;
}
Expand Down
1 change: 1 addition & 0 deletions app/stories/pix-tooltip.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Infobulle en position `top`, fond sombre (par défaut).

<Story of={ComponentStories.Default} height={200} />
<Story of={ComponentStories.WithIcon} height={200} />
<Story of={ComponentStories.WithInput} height={200} />

## Is Light

Expand Down
23 changes: 23 additions & 0 deletions app/stories/pix-tooltip.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ const TemplateWithIconElement = (args) => {
};
};

const TemplateWithInputElement = (args) => {
return {
template: hbs`<PixTooltip @id={{this.id}} @isInline='true'>
<:triggerElement>
<PixInput @id="some-id">
<:label>{{this.label}}</:label>
</PixInput>
</:triggerElement>
<:tooltip>
{{this.text}}
</:tooltip>
</PixTooltip>`,
context: args,
};
};

export const Default = Template.bind({});
Default.args = {
text: 'Hello World',
Expand Down Expand Up @@ -184,6 +201,12 @@ WithIcon.args = {
label: 'À survoler pour voir la tooltip',
};

export const WithInput = TemplateWithInputElement.bind({});
WithInput.args = {
text: 'Hello World',
label: 'À survoler pour voir la tooltip',
};

export const hide = Template.bind({});
hide.args = {
label: "Survoler ici n'affiche pas tooltip",
Expand Down
19 changes: 0 additions & 19 deletions tests/integration/components/pix-tooltip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,6 @@ module('Integration | Component | pix-tooltip', function (hooks) {
assert.notOk(tooltipContentElement);
});

test('it dismissed tooltip on escape keyup', async function (assert) {
// given
const screen = await render(hbs`<PixTooltip>
<:triggerElement>
template block text
</:triggerElement>
<:tooltip></:tooltip>
</PixTooltip>`);

// when
await screen.getByText('template block text').focus();

await userEvent.keyboard('[Escape]');

// then
assert.contains('template block text');
assert.dom('.pix-tooltip__content').isNotVisible();
});

module('tooltip position', function () {
const TOOLTIP_POSITION_SELECTOR = 'pix-tooltip__content--';

Expand Down

0 comments on commit 4e70a8f

Please sign in to comment.