Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inserts slot for unnamed slot text content #65

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/cla.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "CLA Assistant"
on:
issue_comment:
types: [created]
pull_request_target:
types: [opened,closed,synchronize]

permissions:
actions: write
contents: write
pull-requests: write
statuses: write

jobs:
CLAAssistant:
runs-on: ubuntu-latest
steps:
- name: "CLA Assistant"
if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target'
uses: contributor-assistant/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
with:
path-to-signatures: 'signatures/v1/cla.json'
path-to-document: 'https://github.com/enhance-dev/.github/blob/main/CLA.md'
branch: 'main'
allowlist: brianleroux,colepeters,kristoferjoseph,macdonst,ryanbethel,ryanblock,tbeseda
remote-organization-name: enhance-dev
remote-repository-name: .github
56 changes: 41 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,6 @@ function MyTransformScript({ html }) {
console.log(html`<my-transform-script></my-transform-script>`)
```

### `bodyContent`
Enhance SSR outputs an entire valid HTML page but you can pass `bodyContent: true` to get the content of the body element. This can be useful for when you want to isolate output HTML to only the Custom Element you are authoring.

```javaScript
const html = enhance({
bodyContent: true,
elements: {
'my-paragraph': MyParagraph,
}
})
const output = html`
<my-paragraph></my-paragraph>
`
```

### `context`
There are times you will need to pass state to nested child custom elements. To avoid the tedium of passing attributes through multiple levels of nested elements Enhance SSR supplies a `context` object to add state to.

Expand Down Expand Up @@ -283,5 +268,46 @@ export default function MyInstanceID({ html, state }) {

```

### `bodyContent`
Enhance SSR outputs an entire valid HTML page but you can pass `bodyContent: true` to get just the content of the `<body>` element. This can be useful for when you want to isolate the HTML output to just the Custom Element(s) you are authoring.

```javaScript
const html = enhance({
bodyContent: true,
elements: {
'my-paragraph': MyParagraph,
}
})
const output = html`
<my-paragraph></my-paragraph>
`
```

### Integration with `<html>`, `<head>`, and `<body>` elements

Enhance SSR will intelligently merge its rendered `<html>`, `<head>`, and `<body>` elements with any that you provide to it (unless you choose to use the `bodyContent: true` option).

For example:

```js
const html = enhance({
elements: {
'my-content': MyContent,
}
})

html`
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Website</title>
</head>
<body class="foo bar">
<my-content></my-content>
</body>
</html>
`
```

> P.S. Enhance works really well with [Begin](https://begin.com).

19 changes: 18 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,29 @@ function fillSlots(node, template) {
const children = nodeChildren.length
? nodeChildren
: [ ...slot.childNodes ]
const wrappedChildren = children
.filter(child => child.nodeName === '#text'
? child.value.trim().length
: true
)
.map(child => {
return child.nodeName === '#text'
? {
nodeName: 'span',
tagName: 'span',
attrs: [
{ name: 'slot', value: ''}
],
childNodes: [child]
}
: child
})
const slotParentChildNodes = slot.parentNode.childNodes
slotParentChildNodes.splice(
slotParentChildNodes
.indexOf(slot),
1,
...children
...wrappedChildren
)
})

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"type": "module",
"main": "index.mjs",
"scripts": {
"test": "tape-es ./test/enhance.test.mjs | tap-arc"
"test": "tape ./test/enhance.test.mjs | tap-arc"
},
"keywords": [],
"author": "kj <[email protected]>",
"license": "Apache-2.0",
"devDependencies": {
"tap-arc": "^1.2.2",
"tape-es": "^1.2.17"
"tape": "^5.7.5",
"tap-arc": "^1.2.2"
},
"dependencies": {
"@begin/parse5": "^0.0.4",
Expand Down
16 changes: 9 additions & 7 deletions test/enhance.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('default content in unnamed slot', t => {
`
const expected = `
<my-button>
<button>Submit</button>
<button><span slot="">Submit</span></button>
</my-button>
`
t.equal(
Expand Down Expand Up @@ -115,7 +115,7 @@ test('should replace default content in unnamed slot', t => {
`
const expected = `
<my-button>
<button>Let's Go!</button>
<button><span slot="">Let's Go!</span></button>
</my-button>
`
t.equal(
Expand Down Expand Up @@ -304,12 +304,14 @@ test('add authored children to unnamed slot', t => {
const actual = html`
<my-content id="0">
<h4 slot=title>Custom title</h4>
Just text
</my-content>`

const expected = `
<my-content id="0">
<h2>My Content</h2>
<h4 slot="title">Custom title</h4>
<span slot="">Just text</span>
</my-content>

`
Expand Down Expand Up @@ -883,7 +885,7 @@ test('should support nested custom elements with nested slots', t => {
</span>
<my-heading>
<h1>
My Heading
<span slot="">My Heading</span>
</h1>
</my-heading>
</my-super-heading>
Expand Down Expand Up @@ -997,9 +999,9 @@ ${Head()}
<link rel="stylesheet" href="my-link-node-second.css">
</head>
<body>
<my-link-node-first>first</my-link-node-first>
<my-link-node-second>second</my-link-node-second>
<my-link-node-first>first again</my-link-node-first>
<my-link-node-first><span slot="">first</span></my-link-node-first>
<my-link-node-second><span slot="">second</span></my-link-node-second>
<my-link-node-first><span slot="">first again</span></my-link-node-first>
</body>
</html>
`
Expand Down Expand Up @@ -1092,7 +1094,7 @@ test('multiple slots with unnamed slot first', t => {
`
const expected = `
<multiple-slots enhanced="✨">
unnamed slot<div slot="slot1">slot One</div>
<span slot="">unnamed slot</span><div slot="slot1">slot One</div>
</multiple-slots>
`
t.equal(
Expand Down
Loading