VStack
🔰 CSS ⟩ Layout ⟩ System ⟩ 🔸 code ┊問題
Every Layout ⟩ Stack /⭐️
SwiftUI ⟩ VStack
MDN ⟩
HTMLElement ⟩ dataset (: DOMStringMap) - get/set "data-xxx" attributes ⭐️
Element ⟩
CSS ⟩
@import - import styles from other style sheets.
Using data attributes ("data-xxx" attributes) ⭐️
attr() - use element's attributes in CSS ⭐️
version
// 0.0.5 + helpers
import {attr} from './helpers.mjs';
// VStack
export default class VStack extends HTMLElement {
// this.render()
render() {
const {spacing, alignment} = this;
const attrs = [spacing, alignment].filter(attr => !!attr); // remove empty attributes
// if no specific settings, use default values and return.
if (attrs.length === 0) return;
// style ID for this element
const styleID = `VStack-${attrs.join('_')}`;
// put vstack style ID in "data-xxx" attribute
this.dataset.style = styleID;
// if no such <style> with this ID, create it.
if (!document.getElementById(styleID)) {
// create <style>
let style = document.createElement('style');
style.id = styleID;
// concate css rules
const rules = [
// spacing between items
`${spacing ? `[data-style="${styleID}"] > * + :not(i-spacer) {margin-top: ${spacing};}` : ''}`,
// stack alignmentment
`${alignment ? `[data-style="${styleID}"] {align-items: ${alignment};}` : ''}`,
]
.filter(rule => rule !== '') // remove empty rules
.join('\n');
style.innerHTML = `${rules}`;
document.head.appendChild(style);
}
}
// -------------- getters/setters -------------------------
// this.spacing (= '30px')
get spacing() { return attr(this, 'spacing') }
set spacing(val) { return attr(this, 'spacing', val) }
// this.alignment (= 'flex-start' | 'center' | 'flex-end')
get alignment() { return attr(this, 'alignment') }
set alignment(val) { return attr(this, 'alignment', val) }
// -------------- lifecycle callbacks -------------------------
// ⭐ connected to document
connectedCallback() {
this.render();
}
// ⭐ observed attributes
static get observedAttributes() {
return ['spacing', 'alignment'];
}
// re-renders whenever one of attributes changes.
attributeChangedCallback() {
this.render();
}
}
// ⭐️ register <vstack->
customElements.get('vstack-') || customElements.define('vstack-', VStack);
replit ⟩ layout 0.1.0 - align-items: center ➜ stretch
code lab
寫一個 <spacer> 來代替 splitAfter❓(可行)
問題
當在 custom element <i-vstack> 中輸入屬性 align="right",竟然會引發 browser 自動將它轉為:(🤔 align="..." 竟然對 custom element 也有效❓)
導致內部的所有子元素都繼承到此 text-align 性質:

HTML align attribute supports col, colgroup, tbody, td, tfoot, th, thead, tr elements. Usage of align attribute for any other HTML elements is deprecated. You must use CSS for those. 📗 w3resource ⟩ HTML align attribute
In order for a % value to work for height, the parent's height must be determined. 🗣 Why doesn't height: 100% working?
如果都是用百分比,則一路從 html ⟩ body ⟩ .... ⟩ 到自己的直屬母物件都要有明確設定 %,否則只要中間有一層斷掉也是沒用。
TutorialPublic ⟩ How to set the height of a div to 100% using CSS
MDN ⟩ CSS ⟩
Last updated
Was this helpful?