ember-primitives 0.43.0 → 0.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ import Component from "@glimmer/component";
2
+ import type Owner from "@ember/owner";
3
+ export declare class Heading extends Component<{
4
+ Element: HTMLElement;
5
+ Blocks: {
6
+ default: [];
7
+ };
8
+ }> {
9
+ headingScopeAnchor: Text;
10
+ constructor(owner: Owner, args: object);
11
+ get level(): number;
12
+ get hLevel(): string;
13
+ }
14
+ //# sourceMappingURL=heading.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading.d.ts","sourceRoot":"","sources":["../../src/components/heading.gts"],"names":[],"mappings":"AAmEA,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAI3C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAyBtC,qBAAa,OAAQ,SAAQ,SAAS,CAAC;IACrC,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE;QAAE,OAAO,EAAE,EAAE,CAAA;KAAE,CAAC;CACzB,CAAC;IACA,kBAAkB,EAAE,IAAI,CAAC;gBACb,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM;IAMtC,IAAI,KAAK,WAWR;IAED,IAAI,MAAM,WAET;CAqBF"}
@@ -2,6 +2,11 @@ import Component from "@glimmer/component";
2
2
  import type { Newable } from "./type-utils";
3
3
  import type Owner from "@ember/owner";
4
4
  export declare class Provide<Data extends object> extends Component<{
5
+ /**
6
+ * The Element is not customizable
7
+ * (and also sometimes doesn't exist (depending on the `@element` value))
8
+ */
9
+ Element: null;
5
10
  Args: {
6
11
  /**
7
12
  * What data do you want to provide to the DOM subtree?
@@ -18,6 +23,20 @@ export declare class Provide<Data extends object> extends Component<{
18
23
  * With string keys, the `<Consume>` type will be unknown.
19
24
  */
20
25
  key?: string;
26
+ /**
27
+ * Can be used to either customize the element tag ( defaults to div )
28
+ * If set to `false`, we won't use an element for the Provider boundary.
29
+ *
30
+ * Setting this to `false` changes the DOM Node containing the Provider's data to be a text node -- which can be useful when certain CSS situations are needed.
31
+ *
32
+ * But setting to `false` has a hazard: it allows subsequent sibling subtrees to access adjacent providers.
33
+ *
34
+ * There is no way around caveat in library land, and in a framework implementation of context,
35
+ * it can only be solved by having render-tree context implemented, and ignoring DOM
36
+ * (which then makes the only difference between DOM-Context and Context be whether or not
37
+ * the context punches through Portals)
38
+ */
39
+ element?: keyof HTMLElementTagNameMap | false | undefined;
21
40
  };
22
41
  Blocks: {
23
42
  /**
@@ -27,11 +46,12 @@ export declare class Provide<Data extends object> extends Component<{
27
46
  };
28
47
  }> {
29
48
  get data(): Data;
30
- element: Text;
49
+ element: Text | HTMLElement;
31
50
  constructor(owner: Owner, args: {
32
51
  data: Data | (() => Data) | Newable<Data>;
33
52
  key?: string;
34
53
  });
54
+ get useElementProvider(): boolean;
35
55
  }
36
56
  type DataForKey<Key> = Key extends string ? unknown : Key extends Newable<infer T> ? T : Key extends () => infer T ? T : Key;
37
57
  export declare class Consume<Key extends object | string> extends Component<{
@@ -1 +1 @@
1
- {"version":3,"file":"dom-context.d.ts","sourceRoot":"","sources":["../src/dom-context.gts"],"names":[],"mappings":"AAwLA,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAM3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAmBtC,qBAAa,OAAO,CAAC,IAAI,SAAS,MAAM,CAAE,SAAQ,SAAS,CAAC;IAC1D,IAAI,EAAE;QACJ;;;;;WAKG;QACH,IAAI,EAAE,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QAE1C;;;;;;WAMG;QACH,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;IACF,MAAM,EAAE;QACN;;WAEG;QACH,OAAO,EAAE,EAAE,CAAC;KACb,CAAC;CACH,CAAC;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,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,GAAG,EAAE,GAAG,CAAA;KAAE;IAM5C,IACI,OAAO;uBAKK,UAAU,CAAC,GAAG,CAAC;MAa9B;CAOF"}
@@ -0,0 +1,52 @@
1
+
2
+ import Component from '@glimmer/component';
3
+ import { element } from 'ember-element-helper';
4
+ import { precompileTemplate } from '@ember/template-compilation';
5
+ import { setComponentTemplate } from '@ember/component';
6
+
7
+ const LOOKUP = new WeakMap();
8
+ function levelOf(node) {
9
+ let parent = node.parentElement;
10
+ let level = 0;
11
+ while (parent) {
12
+ if (parent instanceof Element) {
13
+ if (parent.tagName.toLowerCase() === "section") {
14
+ level++;
15
+ }
16
+ }
17
+ if (parent instanceof ShadowRoot) {
18
+ parent = parent.host;
19
+ }
20
+ parent = parent.parentNode;
21
+ }
22
+ return level;
23
+ }
24
+ class Heading extends Component {
25
+ headingScopeAnchor;
26
+ constructor(owner, args) {
27
+ super(owner, args);
28
+ this.headingScopeAnchor = document.createTextNode("");
29
+ }
30
+ get level() {
31
+ const existing = LOOKUP.get(this.headingScopeAnchor);
32
+ if (existing) return existing;
33
+ const parentLevel = levelOf(this.headingScopeAnchor);
34
+ const myLevel = parentLevel + 1;
35
+ LOOKUP.set(this.headingScopeAnchor, myLevel);
36
+ return myLevel;
37
+ }
38
+ get hLevel() {
39
+ return `h${this.level}`;
40
+ }
41
+ static {
42
+ setComponentTemplate(precompileTemplate("\n {{this.headingScopeAnchor}}\n\n {{#let (element this.hLevel) as |El|}}\n <El ...attributes>\n {{yield}}\n </El>\n {{/let}}\n ", {
43
+ strictMode: true,
44
+ scope: () => ({
45
+ element
46
+ })
47
+ }), this);
48
+ }
49
+ }
50
+
51
+ export { Heading };
52
+ //# sourceMappingURL=heading.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -2,6 +2,7 @@
2
2
  import Component from '@glimmer/component';
3
3
  import { tracked, cached } from '@glimmer/tracking';
4
4
  import { assert } from '@ember/debug';
5
+ import { isElement } from './narrowing.js';
5
6
  import { createStore } from './store.js';
6
7
  import { precompileTemplate } from '@ember/template-compilation';
7
8
  import { setComponentTemplate } from '@ember/component';
@@ -25,14 +26,26 @@ class Provide extends Component {
25
26
  element;
26
27
  constructor(owner, args) {
27
28
  super(owner, args);
28
- const element = document.createTextNode("");
29
+ assert(`@element may only be \`false\` or a string (or undefined (default when not set))`, this.args.element === undefined || this.args.element === false || typeof this.args.element === "string");
30
+ if (this.useElementProvider) {
31
+ this.element = document.createElement(this.args.element || "div");
32
+ // This tells the browser to ignore everything about this element when it comes to styling
33
+ this.element.style.display = "contents";
34
+ } else {
35
+ this.element = document.createTextNode("");
36
+ }
29
37
  const key = this.args.key ?? this.args.data;
30
- LOOKUP.set(element, [key, () => this.data]);
31
- this.element = element;
38
+ LOOKUP.set(this.element, [key, () => this.data]);
39
+ }
40
+ get useElementProvider() {
41
+ return this.args.element !== false;
32
42
  }
33
43
  static {
34
- setComponentTemplate(precompileTemplate("\n {{this.element}}\n {{yield}}\n ", {
35
- strictMode: true
44
+ setComponentTemplate(precompileTemplate("\n {{#if (isElement this.element)}}\n {{this.element}}\n\n {{#in-element this.element}}\n {{yield}}\n {{/in-element}}\n\n {{else}}\n {{!-- NOTE! This type of provider will _allow_ non-descendents using the same key to find the provider and use it.\n\n For example:\n Provider\n Consumer\n\n Consumer (finds Provider)\n --}}\n\n {{this.element}}\n {{yield}}\n\n {{/if}}\n ", {
45
+ strictMode: true,
46
+ scope: () => ({
47
+ isElement
48
+ })
36
49
  }), this);
37
50
  }
38
51
  }
@@ -40,18 +53,21 @@ class Provide extends Component {
40
53
  * How this works:
41
54
  * - starting at some deep node (Text, Element, whatever),
42
55
  * start crawling up the ancenstry graph (of DOM Nodes).
43
- * - for each DOM node crawled upward, check all previous siblings to see if one matches the requested *key*
44
56
  *
45
- * - This algo "tops out" (since we traverse upwards (otherwise this would be "bottoming out")) at the HTMLDocument (parent of the HTML Tag)
57
+ * - This algo "tops out" (since we traverse upwards (otherwise this would be "bottoming out")) at the HTMLDocument (parent of the HTML Tag)
46
58
  *
47
59
  */
48
60
  function findForKey(startElement, key) {
49
61
  let parent = startElement;
50
62
  while (parent) {
51
63
  let target = parent;
52
- while (target = target?.previousSibling) {
53
- if (!(target instanceof Text)) continue;
64
+ while (target) {
65
+ if (!(target instanceof Element) && !(target instanceof Text)) {
66
+ target = target?.previousSibling;
67
+ continue;
68
+ }
54
69
  const maybe = LOOKUP.get(target);
70
+ target = target?.previousSibling;
55
71
  if (!maybe) {
56
72
  continue;
57
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-primitives",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "Making apps easier to build",
5
5
  "keywords": [
6
6
  "ember-addon"