Skip to content

Commit 2b18ffd

Browse files
authored
fix: add fallback icon to show for slotted whitespace text (#4624)
1 parent 4031cbc commit 2b18ffd

4 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎packages/app-layout/src/vaadin-drawer-toggle.js‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright (c) 2018 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
67
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
78
import { Button } from '@vaadin/button/src/vaadin-button.js';
9+
import { isEmptyTextNode } from '@vaadin/component-base/src/dom-utils.js';
810

911
/**
1012
* The Drawer Toggle component controls the drawer in App Layout component.
@@ -56,9 +58,10 @@ class DrawerToggle extends Button {
5658
top: 12px;
5759
}
5860
</style>
59-
<slot>
61+
<slot id="slot">
6062
<div part="icon"></div>
6163
</slot>
64+
<div part="icon" hidden$="[[!_showFallbackIcon]]"></div>
6265
<slot name="tooltip"></slot>
6366
`;
6467
}
@@ -74,6 +77,12 @@ class DrawerToggle extends Button {
7477
value: 'Toggle navigation panel',
7578
reflectToAttribute: true,
7679
},
80+
81+
/** @private */
82+
_showFallbackIcon: {
83+
type: Boolean,
84+
value: false,
85+
},
7786
};
7887
}
7988

@@ -84,6 +93,23 @@ class DrawerToggle extends Button {
8493
this.dispatchEvent(new CustomEvent('drawer-toggle-click', { bubbles: true, composed: true }));
8594
});
8695
}
96+
97+
/** @protected */
98+
ready() {
99+
super.ready();
100+
101+
this._observer = new FlattenedNodesObserver(this, () => {
102+
this._toggleFallbackIcon();
103+
});
104+
}
105+
106+
/** @private */
107+
_toggleFallbackIcon() {
108+
const nodes = this.$.slot.assignedNodes();
109+
110+
// Show fallback icon if there are 1-2 empty text nodes assigned to the default slot.
111+
this._showFallbackIcon = nodes.length > 0 && nodes.every((node) => isEmptyTextNode(node));
112+
}
87113
}
88114

89115
customElements.define(DrawerToggle.is, DrawerToggle);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots["vaadin-app-layout default"] =
5+
`<slot id="slot">
6+
<div part="icon">
7+
</div>
8+
</slot>
9+
<div
10+
hidden=""
11+
part="icon"
12+
>
13+
</div>
14+
<slot name="tooltip">
15+
</slot>
16+
`;
17+
/* end snapshot vaadin-app-layout default */
18+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync } from '@vaadin/testing-helpers';
3+
import '../../src/vaadin-drawer-toggle.js';
4+
5+
describe('vaadin-app-layout', () => {
6+
let toggle;
7+
8+
beforeEach(() => {
9+
toggle = fixtureSync('<vaadin-drawer-toggle></vaadin-drawer-toggle>');
10+
});
11+
12+
it('default', async () => {
13+
await expect(toggle).shadowDom.to.equalSnapshot();
14+
});
15+
});

‎packages/app-layout/test/drawer-toggle.test.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,57 @@ describe('drawer-toggle', () => {
8787
expect(toggle.getAttribute('aria-label')).to.equal('Label');
8888
});
8989
});
90+
91+
describe('fallback icon', () => {
92+
let icon;
93+
94+
beforeEach(() => {
95+
icon = toggle.shadowRoot.querySelectorAll('[part="icon"]')[1];
96+
});
97+
98+
it('should not show fallback icon by default', () => {
99+
expect(icon.hasAttribute('hidden')).to.be.true;
100+
});
101+
102+
it('should show fallback icon when adding non-empty element', () => {
103+
const div = document.createElement('div');
104+
toggle.appendChild(div);
105+
toggle._observer.flush();
106+
expect(icon.hasAttribute('hidden')).to.be.true;
107+
});
108+
109+
it('should show fallback icon when adding whitespace text node', () => {
110+
const text = document.createTextNode(' ');
111+
toggle.appendChild(text);
112+
toggle._observer.flush();
113+
expect(icon.hasAttribute('hidden')).to.be.false;
114+
});
115+
116+
it('should show fallback icon when adding element to non-default slot', () => {
117+
// Emulate adding element in HTML wrapped with whitespace text nodes
118+
toggle.innerHTML = ' <vaadin-tooltip slot="tooltip"></vaadin-tooltip> ';
119+
toggle._observer.flush();
120+
expect(icon.hasAttribute('hidden')).to.be.false;
121+
});
122+
123+
it('should hide fallback icon when removing whitespace text node', () => {
124+
const text = document.createTextNode(' ');
125+
toggle.appendChild(text);
126+
toggle._observer.flush();
127+
128+
toggle.removeChild(text);
129+
toggle._observer.flush();
130+
expect(icon.hasAttribute('hidden')).to.be.true;
131+
});
132+
133+
it('should hide fallback icon when clearing all slotted content', () => {
134+
// Emulate adding element in HTML wrapped with whitespace text nodes
135+
toggle.innerHTML = ' <vaadin-tooltip slot="tooltip"></vaadin-tooltip> ';
136+
toggle._observer.flush();
137+
138+
toggle.innerHTML = '';
139+
toggle._observer.flush();
140+
expect(icon.hasAttribute('hidden')).to.be.true;
141+
});
142+
});
90143
});

0 commit comments

Comments
 (0)