Skip to content

Commit c3d45f0

Browse files
committed
feat(tooltip): Add toggle method for showing/hiding tooltip with button interaction
1 parent 94b25ed commit c3d45f0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/components/tooltip/tooltip.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
283283
});
284284
};
285285

286+
/** Toggles the tooltip between shown/hidden state after the appropriate delay. */
287+
public toggle = async (): Promise<boolean> => {
288+
return this.open ? this.hide() : this.show();
289+
};
290+
286291
public showWithEvent = async () => {
287292
clearTimeout(this._timeoutId);
288293
if (this.toBeHidden) {

stories/tooltip.stories.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,33 @@ export const Triggers: Story = {
279279
</igc-tooltip>
280280
`,
281281
};
282+
283+
export const Toggle: Story = {
284+
render: () => {
285+
// Use a template ref id to target the tooltip instance
286+
const tooltipId = 'toggle-tooltip';
287+
const buttonId = 'toggle-button';
288+
289+
// Hook into the rendered DOM to attach click listener
290+
setTimeout(() => {
291+
const tooltip = document.getElementById(tooltipId) as IgcTooltipComponent;
292+
const button = document.getElementById(buttonId) as HTMLButtonElement;
293+
294+
if (tooltip && button) {
295+
button.addEventListener('click', () => tooltip.toggle());
296+
}
297+
});
298+
299+
return html`
300+
<igc-button id=${buttonId}>Toggle Tooltip</igc-button>
301+
<igc-tooltip
302+
id=${tooltipId}
303+
placement="bottom"
304+
show-delay="500"
305+
hide-delay="500"
306+
>
307+
This tooltip toggles on button click!
308+
</igc-tooltip>
309+
`;
310+
},
311+
};

0 commit comments

Comments
 (0)