djledda.de main
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

60 satır
1.7 KiB

  1. export function gid(id: string, doc: (Document | ShadowRoot) | undefined) {
  2. return ((doc ?? document).getElementById(id));
  3. }
  4. export function qs(query: string, sub: (HTMLElement | ShadowRoot) | undefined) {
  5. return ((sub ?? document).querySelector(query));
  6. }
  7. export function qsa(query: string, sub?: (HTMLElement | ShadowRoot) | undefined) {
  8. return ((sub ?? document).querySelectorAll(query));
  9. }
  10. export function h<T extends keyof HTMLElementTagNameMap>(
  11. tag: T,
  12. opts?: Partial<HTMLElementTagNameMap[T]> | undefined,
  13. children?: Node[],
  14. ) {
  15. const newEl = Object.assign(document.createElement(tag), opts ?? {});
  16. if (children) {
  17. newEl.append(...children);
  18. }
  19. return newEl;
  20. }
  21. export function html(strs: TemplateStringsArray, ...vals: string[]) {
  22. let result = strs[0];
  23. for (let i = 1; i < strs.length; i++) {
  24. result += vals[i] + strs[i];
  25. }
  26. const template = document.createElement("template");
  27. template.innerHTML = result;
  28. return template;
  29. }
  30. export function css(strs: TemplateStringsArray, ...vals: string[]) {
  31. let result = strs[0];
  32. for (let i = 1; i < strs.length; i++) {
  33. result += vals[i] + strs[i];
  34. }
  35. const sheet = new CSSStyleSheet();
  36. sheet.replaceSync(result);
  37. return sheet;
  38. }
  39. export class DJElement extends HTMLElement {
  40. static styles: CSSStyleSheet;
  41. static template: HTMLTemplateElement;
  42. root: ShadowRoot;
  43. constructor() {
  44. super();
  45. const statics = this.constructor as typeof DJElement;
  46. this.root = this.attachShadow({ mode: "open" });
  47. this.root.appendChild(statics.template.content.cloneNode(true));
  48. this.root.adoptedStyleSheets = statics.styles ? [statics.styles] : [];
  49. }
  50. }