Skip to content

Commit 8b0d454

Browse files
authored
Private props standardization - modifiers (#2583)
1 parent 8325240 commit 8b0d454

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

.changeset/violet-suns-scream.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
Aligned private properties of the HDS modifiers to follow a standardized notation
6+
- `hds-anchored-position`
7+
- `hds-register-event`
8+
- `hds-tooltip`

packages/components/src/modifiers/hds-anchored-position.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,21 @@ export default modifier<HdsAnchoredPositionSignature>(
166166
(element, positional, named = {}) => {
167167
// the element that "floats" next to the "anchor" (whose position is calculated in relation to the anchor)
168168
// notice: this is the element the Ember modifier is attached to
169-
const floatingElement = element;
169+
const _floatingElement = element;
170170

171171
// the element that acts as an "anchor" for the "floating" element
172172
// it can be a DOM (string) selector or a DOM element
173173
// notice: it's expressed as "positional" argument (array of arguments) for the modifier
174174
const _anchorTarget = positional[0];
175-
const anchorElement =
175+
const _anchorElement =
176176
typeof _anchorTarget === 'string'
177177
? document.querySelector(_anchorTarget)
178178
: _anchorTarget;
179179

180180
assert(
181181
'`hds-anchored-position` modifier - the provided "anchoring" element is not defined correctly',
182-
anchorElement instanceof HTMLElement ||
183-
anchorElement instanceof SVGElement
182+
_anchorElement instanceof HTMLElement ||
183+
_anchorElement instanceof SVGElement
184184
);
185185

186186
// the "arrow" element (optional) associated with the "floating" element
@@ -221,14 +221,14 @@ export default modifier<HdsAnchoredPositionSignature>(
221221
// important to know: `computePosition()` is not stateful, it only positions the "floating" element once
222222
// see: https://floating-ui.com/docs/computePosition
223223
const state = await computePosition(
224-
anchorElement,
225-
floatingElement,
224+
_anchorElement,
225+
_floatingElement,
226226
floatingOptions
227227
);
228228

229229
const { x, y, placement, strategy, middlewareData } = state;
230230

231-
Object.assign(floatingElement.style, {
231+
Object.assign(_floatingElement.style, {
232232
position: strategy,
233233
top: `${y}px`,
234234
left: `${x}px`,
@@ -261,8 +261,8 @@ export default modifier<HdsAnchoredPositionSignature>(
261261
// it returns a "cleanup" function that should be invoked when the floating element is removed from the DOM or hidden from the screen.
262262
// see: https://floating-ui.com/docs/autoUpdate
263263
const cleanupFloatingUI = autoUpdate(
264-
anchorElement,
265-
floatingElement,
264+
_anchorElement,
265+
_floatingElement,
266266
computeFloatingPosition
267267
);
268268

packages/components/src/modifiers/hds-register-event.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ export default modifier<HdsRegisterEventSignature>(
2525
(element, positional, named = {}) => {
2626
// the "target" element the listeners are added to
2727
// notice: this is the element the Ember modifier is attached to
28-
const targetElement = element;
28+
const _targetElement = element;
2929
// the event name and handler to apply to the element
3030
// notice: it's expressed as "positional" argument (array) for the modifier
31-
const [event, eventHandler] = positional;
31+
const [_event, _eventHandler] = positional;
3232
// the options for the `addEventListener()` method
3333
// see: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
3434
// notice: it's expressed as "named" argument (object) for the modifier
3535
const { useCapture = false } = named;
3636

37-
targetElement.addEventListener(event, eventHandler, useCapture);
37+
_targetElement.addEventListener(_event, _eventHandler, useCapture);
3838

3939
// this (teardown) function is run when the element is removed from the DOM
4040
return (): void => {
41-
targetElement.removeEventListener(event, eventHandler, useCapture);
41+
_targetElement.removeEventListener(_event, _eventHandler, useCapture);
4242
};
4343
}
4444
);

packages/components/src/modifiers/hds-tooltip.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ export interface HdsTooltipModifierSignature {
3232
}
3333

3434
function cleanup(instance: HdsTooltipModifier): void {
35-
const { interval, needsTabIndex, tooltip } = instance;
36-
if (needsTabIndex) {
37-
tooltip?.reference?.removeAttribute('tabindex');
35+
const { _interval, _needsTabIndex, _tooltip } = instance;
36+
if (_needsTabIndex) {
37+
_tooltip?.reference?.removeAttribute('tabindex');
3838
}
39-
clearInterval(interval);
40-
tooltip?.destroy();
39+
clearInterval(_interval);
40+
_tooltip?.destroy();
4141
}
4242

4343
/**
@@ -54,10 +54,10 @@ function cleanup(instance: HdsTooltipModifier): void {
5454
*
5555
*/
5656
export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSignature> {
57-
didSetup = false;
58-
interval: number | undefined = undefined;
59-
needsTabIndex = false;
60-
tooltip: TippyInstance | undefined = undefined;
57+
private _didSetup = false;
58+
_interval: number | undefined = undefined;
59+
_needsTabIndex = false;
60+
_tooltip: TippyInstance | undefined = undefined;
6161

6262
constructor(owner: unknown, args: ArgsFor<HdsTooltipModifierSignature>) {
6363
super(owner, args);
@@ -92,9 +92,9 @@ export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSigna
9292
): void {
9393
assert('Tooltip must have an element', element);
9494

95-
if (!this.didSetup) {
95+
if (!this._didSetup) {
9696
this.#setup(element, positional, named);
97-
this.didSetup = true;
97+
this._didSetup = true;
9898
}
9999

100100
this.#update(element, positional, named);
@@ -106,7 +106,7 @@ export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSigna
106106
named: HdsTooltipModifierSignature['Args']['Named']
107107
): void {
108108
const tooltipProps = this.#getTooltipProps(element, positional, named);
109-
this.tooltip = tippy(element, tooltipProps);
109+
this._tooltip = tippy(element, tooltipProps);
110110
}
111111

112112
#update(
@@ -115,7 +115,7 @@ export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSigna
115115
named: HdsTooltipModifierSignature['Args']['Named']
116116
): void {
117117
const tooltipProps = this.#getTooltipProps(element, positional, named);
118-
this.tooltip?.setProps(tooltipProps);
118+
this._tooltip?.setProps(tooltipProps);
119119
}
120120

121121
#getTooltipProps(
@@ -167,8 +167,8 @@ export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSigna
167167
if (Array.isArray(delay) && delay.length) {
168168
if (typeof delay[1] !== 'undefined') {
169169
options.onShown = (tooltip) => {
170-
clearInterval(this.interval);
171-
this.interval = setTimeout(() => {
170+
clearInterval(this._interval);
171+
this._interval = setTimeout(() => {
172172
tooltip.hide();
173173
}, delay[1] ?? 0);
174174
};
@@ -179,7 +179,7 @@ export default class HdsTooltipModifier extends Modifier<HdsTooltipModifierSigna
179179
const $trigger = $anchor;
180180

181181
if (!$trigger.hasAttribute('tabindex')) {
182-
this.needsTabIndex = true;
182+
this._needsTabIndex = true;
183183
$trigger.setAttribute('tabindex', '0');
184184
}
185185

0 commit comments

Comments
 (0)