microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/ux/atoms/utils.ts
32lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // **** Helper functions for rendering SVG elements **** |
| 5 | |
| 6 | type StringMap = Record<string, string>; |
| 7 | |
| 8 | export function createSvgElements(...tags: string[]): SVGElement[] { |
| 9 | return tags.map((tag) => |
| 10 | document.createElementNS("http://www.w3.org/2000/svg", tag), |
| 11 | ); |
| 12 | } |
| 13 | |
| 14 | export function setAttributes(el: SVGElement, attrs: StringMap) { |
| 15 | for (const key in attrs) el.setAttribute(key, attrs[key]); |
| 16 | } |
| 17 | |
| 18 | export function appendChildren(parent: Element, children: Element[]) { |
| 19 | children.forEach((child) => parent.appendChild(child)); |
| 20 | } |
| 21 | |
| 22 | export function addChildWithClass( |
| 23 | parent: HTMLElement, |
| 24 | childTag: string, |
| 25 | className: string, |
| 26 | ): HTMLElement { |
| 27 | const parentDoc = parent.ownerDocument; |
| 28 | const child = parentDoc.createElement(childTag); |
| 29 | child.classList.add(className); |
| 30 | parent.appendChild(child); |
| 31 | return child; |
| 32 | } |
| 33 | |