11. CSS - attachShadow
class Note {
constructor({ x = 100, y = 100, message = "New note" }) {
this.setThisContext();
const handleBarEl = document.createElement("div");
handleBarEl.classList.add("handle-bar");
handleBarEl.addEventListener("mousedown", this.startDragHandler);
this.messageEl = document.createElement("textarea");
this.messageEl.classList.add("message");
this.messageEl.value = message;
const sheet = new CSSStyleSheet();
sheet.replaceSync(`
:host {
background-color: lightgoldenrodyellow;
display: flex;
flex-direction: column;
position: fixed;
left: calc(var(--x) * 1px);
top: calc(var(--y) * 1px);
width: 250px;
height: 250px;
}
.handle-bar {
background-color: var(--handle-bar-color, tomato);
cursor: pointer;
flex: 0 0 20px;
}
.message {
flex: 1 0;
outline: none;
padding: 8px;
resize: none;
}
`);
this.rootEl = document.createElement("div");
const shadow = this.rootEl.attachShadow({ mode: "open" });
shadow.append(handleBarEl, this.messageEl);
shadow.adoptedStyleSheets.push(sheet);
this.updatePosition(x, y);
}
}Last updated
Was this helpful?