Skip to content

Commit 7416cc3

Browse files
committed
refactor(action-bar, action-pad, action-group): Remove use of getSlotted utility. #6059
1 parent 9eedc6e commit 7416cc3

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

packages/calcite-components/src/components/action-bar/action-bar.tsx

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
connectConditionalSlotComponent,
1818
disconnectConditionalSlotComponent,
1919
} from "../../utils/conditionalSlot";
20-
import { focusFirstTabbable, getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
20+
import {
21+
focusFirstTabbable,
22+
slotChangeGetAssignedElements,
23+
slotChangeHasAssignedElement,
24+
} from "../../utils/dom";
2125
import {
2226
componentFocusable,
2327
LoadableComponent,
@@ -176,6 +180,10 @@ export class ActionBar
176180

177181
@State() effectiveLocale: string;
178182

183+
@State() hasActionsEnd = false;
184+
185+
@State() expandTooltip: HTMLCalciteTooltipElement;
186+
179187
@Watch("effectiveLocale")
180188
effectiveLocaleChange(): void {
181189
updateMessages(this, this.effectiveLocale);
@@ -297,9 +305,7 @@ export class ActionBar
297305
this.setGroupLayout(actionGroups);
298306

299307
const groupCount =
300-
getSlotted(el, SLOTS.actionsEnd) || getSlotted(el, SLOTS.bottomActions) || !expandDisabled
301-
? actionGroups.length + 1
302-
: actionGroups.length;
308+
this.hasActionsEnd || !expandDisabled ? actionGroups.length + 1 : actionGroups.length;
303309

304310
const { actionHeight, actionWidth } = geActionDimensions(actions);
305311

@@ -345,6 +351,18 @@ export class ActionBar
345351
this.setGroupLayout(groups);
346352
};
347353

354+
handleActionsEndSlotChange = (event: Event): void => {
355+
this.hasActionsEnd = slotChangeHasAssignedElement(event);
356+
};
357+
358+
handleTooltipSlotChange = (event: Event): void => {
359+
const tooltips = slotChangeGetAssignedElements(event).filter((el) =>
360+
el?.matches("calcite-tooltip")
361+
) as HTMLCalciteTooltipElement[];
362+
363+
this.expandTooltip = tooltips[0];
364+
};
365+
348366
// --------------------------------------------------------------------------
349367
//
350368
// Render Methods
@@ -354,8 +372,6 @@ export class ActionBar
354372
renderBottomActionGroup(): VNode {
355373
const { expanded, expandDisabled, el, position, toggleExpand, scale, layout, messages } = this;
356374

357-
const tooltip = getSlotted(el, SLOTS.expandTooltip) as HTMLCalciteTooltipElement;
358-
359375
const expandToggleNode = !expandDisabled ? (
360376
<ExpandToggle
361377
collapseText={messages.collapse}
@@ -365,22 +381,25 @@ export class ActionBar
365381
position={position}
366382
scale={scale}
367383
toggle={toggleExpand}
368-
tooltip={tooltip}
384+
tooltip={this.expandTooltip}
369385
// eslint-disable-next-line react/jsx-sort-props
370386
ref={this.setExpandToggleRef}
371387
/>
372388
) : null;
373389

374-
return getSlotted(el, SLOTS.actionsEnd) ||
375-
getSlotted(el, SLOTS.bottomActions) ||
376-
expandToggleNode ? (
377-
<calcite-action-group class={CSS.actionGroupEnd} layout={layout} scale={scale}>
378-
<slot name={SLOTS.actionsEnd} />
379-
<slot name={SLOTS.bottomActions} />
380-
<slot name={SLOTS.expandTooltip} />
390+
return (
391+
<calcite-action-group
392+
class={CSS.actionGroupEnd}
393+
hidden={this.expandDisabled && !this.hasActionsEnd}
394+
layout={layout}
395+
scale={scale}
396+
>
397+
<slot name={SLOTS.actionsEnd} onSlotchange={this.handleActionsEndSlotChange} />
398+
<slot name={SLOTS.bottomActions} onSlotchange={this.handleActionsEndSlotChange} />
399+
<slot name={SLOTS.expandTooltip} onSlotchange={this.handleTooltipSlotChange} />
381400
{expandToggleNode}
382401
</calcite-action-group>
383-
) : null;
402+
);
384403
}
385404

386405
render(): VNode {

packages/calcite-components/src/components/action-group/action-group.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
connectConditionalSlotComponent,
66
disconnectConditionalSlotComponent,
77
} from "../../utils/conditionalSlot";
8-
import { getSlotted } from "../../utils/dom";
98
import {
109
componentFocusable,
1110
LoadableComponent,
@@ -25,6 +24,7 @@ import { Columns, Layout, Scale } from "../interfaces";
2524
import { ActionGroupMessages } from "./assets/action-group/t9n";
2625
import { ICONS, SLOTS } from "./resources";
2726
import { OverlayPositioning } from "../../utils/floating-ui";
27+
import { slotChangeHasAssignedElement } from "../../utils/dom";
2828

2929
/**
3030
* @slot - A slot for adding a group of `calcite-action`s.
@@ -124,6 +124,8 @@ export class ActionGroup
124124

125125
@State() defaultMessages: ActionGroupMessages;
126126

127+
@State() hasMenuActions = false;
128+
127129
//--------------------------------------------------------------------------
128130
//
129131
// Public Methods
@@ -169,22 +171,15 @@ export class ActionGroup
169171
//
170172
// --------------------------------------------------------------------------
171173

172-
renderTooltip(): VNode {
173-
const { el } = this;
174-
const hasTooltip = getSlotted(el, SLOTS.menuTooltip);
175-
176-
return hasTooltip ? <slot name={SLOTS.menuTooltip} slot={ACTION_MENU_SLOTS.tooltip} /> : null;
177-
}
178-
179174
renderMenu(): VNode {
180-
const { el, expanded, menuOpen, scale, layout, messages, overlayPositioning } = this;
181-
182-
const hasMenuItems = getSlotted(el, SLOTS.menuActions);
175+
const { expanded, menuOpen, scale, layout, messages, overlayPositioning, hasMenuActions } =
176+
this;
183177

184-
return hasMenuItems ? (
178+
return (
185179
<calcite-action-menu
186180
expanded={expanded}
187181
flipPlacements={["left", "right"]}
182+
hidden={!hasMenuActions}
188183
label={messages.more}
189184
onCalciteActionMenuOpen={this.setMenuOpen}
190185
open={menuOpen}
@@ -199,10 +194,10 @@ export class ActionGroup
199194
text={messages.more}
200195
textEnabled={expanded}
201196
/>
202-
<slot name={SLOTS.menuActions} />
203-
{this.renderTooltip()}
197+
<slot name={SLOTS.menuActions} onSlotchange={this.handleMenuActionsSlotChange} />
198+
<slot name={SLOTS.menuTooltip} slot={ACTION_MENU_SLOTS.tooltip} />
204199
</calcite-action-menu>
205-
) : null;
200+
);
206201
}
207202

208203
render(): VNode {
@@ -223,4 +218,8 @@ export class ActionGroup
223218
setMenuOpen = (event: CalciteActionMenuCustomEvent<void>): void => {
224219
this.menuOpen = !!(event.target as HTMLCalciteActionMenuElement).open;
225220
};
221+
222+
handleMenuActionsSlotChange = (event: Event): void => {
223+
this.hasMenuActions = slotChangeHasAssignedElement(event);
224+
};
226225
}

packages/calcite-components/src/components/action-pad/action-pad.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
connectConditionalSlotComponent,
1717
disconnectConditionalSlotComponent,
1818
} from "../../utils/conditionalSlot";
19-
import { getSlotted, slotChangeGetAssignedElements } from "../../utils/dom";
19+
import { slotChangeGetAssignedElements } from "../../utils/dom";
2020
import {
2121
componentFocusable,
2222
LoadableComponent,
@@ -131,6 +131,8 @@ export class ActionPad
131131

132132
@Element() el: HTMLCalciteActionPadElement;
133133

134+
@State() expandTooltip: HTMLCalciteTooltipElement;
135+
134136
mutationObserver = createObserver("mutation", () =>
135137
this.setGroupLayout(Array.from(this.el.querySelectorAll("calcite-action-group")))
136138
);
@@ -233,6 +235,14 @@ export class ActionPad
233235
this.setGroupLayout(groups);
234236
};
235237

238+
handleTooltipSlotChange = (event: Event): void => {
239+
const tooltips = slotChangeGetAssignedElements(event).filter((el) =>
240+
el?.matches("calcite-tooltip")
241+
) as HTMLCalciteTooltipElement[];
242+
243+
this.expandTooltip = tooltips[0];
244+
};
245+
236246
// --------------------------------------------------------------------------
237247
//
238248
// Component Methods
@@ -242,8 +252,6 @@ export class ActionPad
242252
renderBottomActionGroup(): VNode {
243253
const { expanded, expandDisabled, messages, el, position, toggleExpand, scale, layout } = this;
244254

245-
const tooltip = getSlotted(el, SLOTS.expandTooltip) as HTMLCalciteTooltipElement;
246-
247255
const expandToggleNode = !expandDisabled ? (
248256
<ExpandToggle
249257
collapseText={messages.collapse}
@@ -253,15 +261,15 @@ export class ActionPad
253261
position={position}
254262
scale={scale}
255263
toggle={toggleExpand}
256-
tooltip={tooltip}
264+
tooltip={this.expandTooltip}
257265
// eslint-disable-next-line react/jsx-sort-props
258266
ref={this.setExpandToggleRef}
259267
/>
260268
) : null;
261269

262270
return expandToggleNode ? (
263271
<calcite-action-group class={CSS.actionGroupEnd} layout={layout} scale={scale}>
264-
<slot name={SLOTS.expandTooltip} />
272+
<slot name={SLOTS.expandTooltip} onSlotchange={this.handleTooltipSlotChange} />
265273
{expandToggleNode}
266274
</calcite-action-group>
267275
) : null;

0 commit comments

Comments
 (0)