-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add middle and end slots to horizontal layout (#8515)
- Loading branch information
1 parent
a14ed5e
commit 1470d9c
Showing
18 changed files
with
542 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Horizontal layout</title> | ||
<script type="module" src="./common.js"></script> | ||
|
||
<script type="module"> | ||
import '@vaadin/horizontal-layout'; | ||
</script> | ||
</head> | ||
|
||
<body> | ||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 600px;" theme="spacing wrap"> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
</vaadin-horizontal-layout> | ||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 350px;" theme="spacing wrap"> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
</vaadin-horizontal-layout> | ||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 350px;" theme="spacing wrap"> | ||
<div style="background: #90ee90; width: 200px">Start</div> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
</vaadin-horizontal-layout> | ||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 400px;" theme="spacing wrap"> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
</vaadin-horizontal-layout> | ||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 250px;" theme="spacing wrap"> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div style="background: #90ee90; width: 100px">Start</div> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
</vaadin-horizontal-layout> | ||
|
||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 400px;" theme="spacing wrap"> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
</vaadin-horizontal-layout> | ||
|
||
<br /> | ||
|
||
<vaadin-horizontal-layout style="border: solid 1px blue; width: 250px;" theme="spacing wrap"> | ||
<div slot="middle" style="background: #ffd700;">Middle</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
<div slot="end" style="background: #f08080; width: 100px">End</div> | ||
</vaadin-horizontal-layout> | ||
</body> | ||
</html> |
8 changes: 8 additions & 0 deletions
8
packages/horizontal-layout/src/vaadin-horizontal-layout-mixin.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2017 - 2025 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import type { Constructor } from '@open-wc/dedupe-mixin'; | ||
|
||
export declare function HorizontalLayoutMixin<T extends Constructor<HTMLElement>>(base: T): T; |
85 changes: 85 additions & 0 deletions
85
packages/horizontal-layout/src/vaadin-horizontal-layout-mixin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2017 - 2025 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import { isEmptyTextNode } from '@vaadin/component-base/src/dom-utils.js'; | ||
import { SlotObserver } from '@vaadin/component-base/src/slot-observer.js'; | ||
|
||
/** | ||
* @polymerMixin | ||
*/ | ||
export const HorizontalLayoutMixin = (superClass) => | ||
class extends superClass { | ||
/** @protected */ | ||
ready() { | ||
super.ready(); | ||
|
||
const startSlot = this.shadowRoot.querySelector('slot:not([name])'); | ||
this.__startSlotObserver = new SlotObserver(startSlot, ({ currentNodes, removedNodes }) => { | ||
if (removedNodes.length) { | ||
this.__clearAttribute(removedNodes, 'last-start-child'); | ||
} | ||
|
||
const children = currentNodes.filter((node) => node.nodeType === Node.ELEMENT_NODE); | ||
this.__updateAttributes(children, 'start', false, true); | ||
|
||
const nodes = currentNodes.filter((node) => !isEmptyTextNode(node)); | ||
this.toggleAttribute('has-start', nodes.length > 0); | ||
}); | ||
|
||
const endSlot = this.shadowRoot.querySelector('[name="end"]'); | ||
this.__endSlotObserver = new SlotObserver(endSlot, ({ currentNodes, removedNodes }) => { | ||
if (removedNodes.length) { | ||
this.__clearAttribute(removedNodes, 'first-end-child'); | ||
} | ||
|
||
this.__updateAttributes(currentNodes, 'end', true, false); | ||
|
||
this.toggleAttribute('has-end', currentNodes.length > 0); | ||
}); | ||
|
||
const middleSlot = this.shadowRoot.querySelector('[name="middle"]'); | ||
this.__middleSlotObserver = new SlotObserver(middleSlot, ({ currentNodes, removedNodes }) => { | ||
if (removedNodes.length) { | ||
this.__clearAttribute(removedNodes, 'first-middle-child'); | ||
this.__clearAttribute(removedNodes, 'last-middle-child'); | ||
} | ||
|
||
this.__updateAttributes(currentNodes, 'middle', true, true); | ||
|
||
this.toggleAttribute('has-middle', currentNodes.length > 0); | ||
}); | ||
} | ||
|
||
/** @private */ | ||
__clearAttribute(nodes, attr) { | ||
const el = nodes.find((node) => node.nodeType === Node.ELEMENT_NODE && node.hasAttribute(attr)); | ||
if (el) { | ||
el.removeAttribute(attr); | ||
} | ||
} | ||
|
||
/** @private */ | ||
__updateAttributes(nodes, slot, setFirst, setLast) { | ||
nodes.forEach((child, idx) => { | ||
if (setFirst) { | ||
const attr = `first-${slot}-child`; | ||
if (idx === 0) { | ||
child.setAttribute(attr, ''); | ||
} else if (child.hasAttribute(attr)) { | ||
child.removeAttribute(attr); | ||
} | ||
} | ||
|
||
if (setLast) { | ||
const attr = `last-${slot}-child`; | ||
if (idx === nodes.length - 1) { | ||
child.setAttribute(attr, ''); | ||
} else if (child.hasAttribute(attr)) { | ||
child.removeAttribute(attr); | ||
} | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.