Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit c5e18b0

Browse files
sayriscopybara-github
authored andcommitted
feat(tooltip): Expose hide and isShown methods in the MDCTooltip component. This allows MDC clients to create their own class to enforce only one tooltip being shown at a time.
PiperOrigin-RevId: 350395701
1 parent 9687353 commit c5e18b0

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

packages/mdc-tooltip/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ Mixin | Description
166166
Method Signature | Description
167167
--- | ---
168168
`setTooltipPosition(position: {xPos?: XPosition, yPos?: YPosition}) => void` | Specify how the tooltip should be aligned with the anchor element. See [tooltip positioning](#tooltip-positioning) section for more information.
169-
`setAnchorBoundaryType(type: AnchorBoundaryType) => void` | Specify whether the anchor element is `bounded` (element has an identifiable boundary such as a button) or `unbounded` (element does not have a visually declared boundary such as a text link). Tooltips are placed closer to bounded anchor elements compared to unbounded anchor elements. If no type is specified, defaults to `bounded`
169+
`setAnchorBoundaryType(type: AnchorBoundaryType) => void` | Specify whether the anchor element is `bounded` (element has an identifiable boundary such as a button) or `unbounded` (element does not have a visually declared boundary such as a text link). Tooltips are placed closer to bounded anchor elements compared to unbounded anchor elements. If no type is specified, defaults to `bounded`.
170+
`hide() => void` | Proxies to the foundation's `hide` method, immediately hides the tooltip if it is shown.
171+
`isShown() => boolean` | Returns whether or not the tooltip is shown.
170172

171173
### Usage Within Frameworks
172174

packages/mdc-tooltip/component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ export class MDCTooltip extends MDCComponent<MDCTooltipFoundation> {
127127
this.foundation.setAnchorBoundaryType(type);
128128
}
129129

130+
hide() {
131+
this.foundation.hide();
132+
}
133+
134+
isShown() {
135+
this.foundation.getIsShown();
136+
}
137+
130138
getDefaultFoundation() {
131139
const adapter: MDCTooltipAdapter = {
132140
getAttribute: (attr) => this.root.getAttribute(attr),

packages/mdc-tooltip/foundation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export class MDCTooltipFoundation extends MDCFoundation<MDCTooltipAdapter> {
145145
this.adapter.getAnchorAttribute(attributes.ARIA_HASPOPUP) === 'true';
146146
}
147147

148+
getIsShown() {
149+
return this.isShown;
150+
}
151+
148152
getIsRich() {
149153
return this.isRich;
150154
}

packages/mdc-tooltip/test/component.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ describe('MDCTooltip', () => {
192192
component.destroy();
193193
});
194194

195+
it('#hide forwards to MDCFoundation#hide', () => {
196+
const {mockFoundation, component} = setupTestWithMockFoundation(fixture);
197+
component.hide();
198+
expect(mockFoundation.hide).toHaveBeenCalled();
199+
component.destroy();
200+
});
201+
202+
it('#isShown forwards to MDCFoundation#getIsShown', () => {
203+
const {mockFoundation, component} = setupTestWithMockFoundation(fixture);
204+
component.isShown();
205+
expect(mockFoundation.getIsShown).toHaveBeenCalled();
206+
component.destroy();
207+
});
208+
195209
it('sets aria-hidden to false when showing tooltip on an anchor annotated with `aria-describedby`',
196210
() => {
197211
const tooltipElem = fixture.querySelector<HTMLElement>('#tt0')!;

packages/mdc-tooltip/test/foundation.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ describe('MDCTooltipFoundation', () => {
172172
expect(foundation.getIsPersistent()).toBeTrue();
173173
});
174174

175+
it('#getIsShown returns true if the tooltip is currently shown', () => {
176+
const {foundation} = setUpFoundationTest(MDCTooltipFoundation);
177+
foundation.show();
178+
179+
expect(foundation.getIsShown()).toBeTrue();
180+
});
181+
182+
it('#getIsShown returns false if the tooltip is currently hidden', () => {
183+
const {foundation} = setUpFoundationTest(MDCTooltipFoundation);
184+
foundation.show();
185+
foundation.hide();
186+
187+
expect(foundation.getIsShown()).toBeFalse();
188+
});
189+
175190
it('#show modifies tooltip element so it is shown', () => {
176191
const {foundation, mockAdapter} = setUpFoundationTest(MDCTooltipFoundation);
177192
foundation.show();

0 commit comments

Comments
 (0)