1.7.7 • Published 2 months ago

@blockquote-web-components/blockquote-mixin-slot-content v1.7.7

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

Lit

BlockquoteMixinSlotContent is a mixin for managing the flattened set of nodes assigned to a slot when the node(s) contained in some slot change.

It implements the event handling pattern called event delegation.

Demo

Open in StackBlitz

Example:

class SlotElement extends BlockquoteMixinSlotContent(LitElement) {
  // ...
  constructor() {
    super();
    this.addEventListener('slotchanges', this._onSlotChanges);
  }

  _onSlotChanges = (ev) => {
    const { detail } = ev;
    console.log(detail);
  }
  // ...
}

Caveats with whitespace:

It's important to understand some of the nuances between text nodes that contain text and text nodes that contain only whitespace.

<slot-element>
  nodeText1
  <p>Element 1</p>
  <p>Element 2</p>
</slot-element>

assignedNodes returns 5 Nodes

assignedNodes: Array(5)
0: text // textContent: "\n        nodeText1\n        "
1: p    // textContent: "Element 1"
2: text // textContent: "\n        "
3: p    // textContent: "Element 2"
4: text // textContent: "\n        "
length: 5

Also, another problem with whitespace is that when the content is just whitespace, assignedNodes are no longer able to return flattened nodes.

without whitespace

<slot-element>
  #shadow-root
  <slot>
    <img src="icon.svg" />
  </slot>
</slot-element>

<slot-element>Hello</slot-element>
document.querySelector('slot-element').textContent = '';

// assignedNodes [`img`]

with whitespace

<slot-element>
  #shadow-root
  <slot>
    <img src="icon.svg" />
  </slot>
</slot-element>

<slot-element>Hello</slot-element>
document.querySelector('slot-element').textContent = ' ';

// assignedNodes [`#text`]
// * missing `flattened` node

The detail property - keeping that in mind.

The mixin will return only nodes whose content is not whitespace and equally for flattened nodes.

<slot-element>
  #shadow-root
  <slot>
    <img src="icon.svg" />
  </slot>
</slot-element>

<slot-element>
  <p>sdfas</p>
  <p>2jfie</p>
</slot-element>

event.detail

{
  "assignedSlotContent": {
    "slotName": "",
    "assignedSlot": slot
  },
  "assignedNodesContent": {
    "assignedNodesByNode": [
      {
        "flatten": false,
        "assignedNodes": p,
        "assignedSlot": slot
      },
      {
        "flatten": false,
        "assignedNodes": p,
        "assignedSlot": slot
      }
    ],
    "assignedNodes": [p, p]
  },
  "flattenedNodesContent": {
    "assignedNodesByNode": [
      {
        "flatten": true,
        "assignedNodes": img,
        "assignedSlot": null
      }
    ],
    "assignedNodes": [img]
  },
  "originalEvent": {
    "event": {
      "isTrusted": true,
      "type": "slotchange",
      "target": null,
      "currentTarget": null,
      // ...
    },
    "assignedNodes": [text, p, text, p, text, text, text]
  }
}
<slot-element>
  #shadow-root
  <slot></slot>
</slot-element>

<slot-element> </slot-element>

event.detail

{
  "assignedSlotContent": {
    "slotName": "",
    "assignedSlot": null
  },
  "assignedNodesContent": {
    "assignedNodesByNode": [],
    "assignedNodes": []
  },
  "flattenedNodesContent": {
    "assignedNodesByNode": [],
    "assignedNodes": []
  },
  "originalEvent": {
    "event": {
      "isTrusted": true,
      "type": "slotchange",
      "target": null,
      "currentTarget": null,
      // ...
    },
    "assignedNodes": [text]
  }
}

src/BlockquoteMixinSlotContent.js:

mixin: BlockquoteMixinSlotContent

Mixins
NameModulePackage
dedupeMixin@open-wc/dedupe-mixin
Parameters
NameTypeDefaultDescription
Base
Fields
NamePrivacyTypeDefaultDescriptionInherited From
_onSlotChange

Exports

KindNameDeclarationModulePackage
jsBlockquoteMixinSlotContentBlockquoteMixinSlotContentsrc/BlockquoteMixinSlotContent.js

src/index.js:

Exports

KindNameDeclarationModulePackage
jsBlockquoteMixinSlotContentBlockquoteMixinSlotContent./BlockquoteMixinSlotContent.js
1.7.7

2 months ago

1.7.6

2 months ago

1.7.5

3 months ago

1.7.4

3 months ago

1.7.3

3 months ago

1.7.2

4 months ago

1.7.1

5 months ago

1.6.2

5 months ago

1.5.3

6 months ago

1.7.0

5 months ago

1.6.1

5 months ago

1.5.2

6 months ago

1.6.0

6 months ago

1.5.1

7 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.3.8

11 months ago

1.3.7

11 months ago

1.3.6

11 months ago

1.3.5

11 months ago

1.3.4

11 months ago

1.3.3

12 months ago

1.3.2

1 year ago

1.3.1

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.3.0

1 year ago

1.2.9

1 year ago

1.2.12

1 year ago

1.2.13

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.14

1 year ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.0

2 years ago

1.1.8

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago