Skip to content

Add support for slotted elements (fixes #916) #917

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

Open
wants to merge 1 commit 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
41 changes: 41 additions & 0 deletions integration/__snapshots__/nested_slots.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`target in nested slots into scrollable container in shadow DOM block: "nearest" 1`] = `
[
{
"el": "div.second-inner-container",
"left": 0,
"top": 100,
},
{
"el": "div.first-inner-container",
"left": 0,
"top": 600,
},
{
"el": "html",
"left": 0,
"top": 0,
},
]
`;

exports[`target in nested slots into scrollable container in shadow DOM block: "start" 1`] = `
[
{
"el": "div.second-inner-container",
"left": 0,
"top": 600,
},
{
"el": "div.first-inner-container",
"left": 0,
"top": 600,
},
{
"el": "html",
"left": 0,
"top": 0,
},
]
`;
31 changes: 31 additions & 0 deletions integration/__snapshots__/slot.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`target slotted into scrollable container in shadow DOM block: "nearest" 1`] = `
[
{
"el": "div.inner-container",
"left": 0,
"top": 100,
},
{
"el": "html",
"left": 0,
"top": 0,
},
]
`;

exports[`target slotted into scrollable container in shadow DOM block: "start" 1`] = `
[
{
"el": "div.inner-container",
"left": 0,
"top": 600,
},
{
"el": "html",
"left": 0,
"top": 0,
},
]
`;
76 changes: 76 additions & 0 deletions integration/nested_slots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<script type="module" src="./utils.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
}
.outer-container {
height: 100%;
width: 100%;
overflow: clip;
}
.target {
background: crimson;
height: 100px;
width: 100px;
}
</style>
<div class="outer-container">
<template shadowrootmode="open">
<style>
.outer-container {
height: 100%;
width: 100%;
overflow: clip;
}
.first-inner-container {
height: 100vh;
background: whitesmoke;
overflow: auto;
}
.scroller {
height: 200vh;
width: 90%;
padding-top: 100vh;
box-sizing: border-box;
}
</style>
<div class="first-inner-container">
<div class="scroller">
<div class="outer-container">
<template shadowrootmode="open">
<style>
.second-inner-container {
height: 100vh;
background: whitesmoke;
overflow: auto;
}
.scroller {
height: 200vh;
width: 90%;
padding-top: 100vh;
box-sizing: border-box;
}
</style>
<div class="second-inner-container">
<div class="scroller">
<slot></slot>
</div>
</div>
</template>
<slot></slot>
</div>
</div>
</div>
</template>
<div class="target"></div>
</div>
37 changes: 37 additions & 0 deletions integration/nested_slots.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
beforeAll(async () => {
await page.goto('http://localhost:3000/integration/nested_slots')
})

describe('target in nested slots into scrollable container in shadow DOM', () => {
test('block: "nearest"', async () => {
expect.assertions(4)
const windowHeight = await page.evaluate(() => window.innerHeight)
const actual = await page.evaluate(() => {
return window
.computeScrollIntoView(document.querySelector('.target'), {
block: 'nearest',
})
.map(window.mapActions)
})
expect(actual).toHaveLength(3)
expect(actual[0]).toMatchObject({ el: 'div.second-inner-container', left: 0, top: 100 })
expect(actual[1]).toMatchObject({ el: 'div.first-inner-container', left: 0, top: windowHeight })
expect(actual).toMatchSnapshot()
})

test('block: "start"', async () => {
expect.assertions(4)
const windowHeight = await page.evaluate(() => window.innerHeight)
const actual = await page.evaluate(() => {
return window
.computeScrollIntoView(document.querySelector('.target'), {
block: 'start',
})
.map(window.mapActions)
})
expect(actual).toHaveLength(3)
expect(actual[0]).toMatchObject({ el: 'div.second-inner-container', left: 0, top: windowHeight })
expect(actual[1]).toMatchObject({ el: 'div.first-inner-container', left: 0, top: windowHeight })
expect(actual).toMatchSnapshot()
})
})
49 changes: 49 additions & 0 deletions integration/slot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<script type="module" src="./utils.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
}
.outer-container {
height: 100vh;
width: 100vw;
overflow: clip;
}
.target {
background: crimson;
height: 100px;
width: 100px;
}
</style>
<div class="outer-container">
<template shadowrootmode="open">
<style>
.inner-container {
height: 100vh;
background: whitesmoke;
overflow: auto;
}

.scroller {
height: 200vh;
padding-top: 100vh;
box-sizing: border-box;
}
</style>
<div class="inner-container">
<div class="scroller">
<slot></slot>
</div>
</div>
</template>
<div class="target"></div>
</div>
34 changes: 34 additions & 0 deletions integration/slot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
beforeAll(async () => {
await page.goto('http://localhost:3000/integration/slot')
})

describe('target slotted into scrollable container in shadow DOM', () => {
test('block: "nearest"', async () => {
expect.assertions(3)
const actual = await page.evaluate(() => {
return window
.computeScrollIntoView(document.querySelector('.target'), {
block: 'nearest',
})
.map(window.mapActions)
})
expect(actual).toHaveLength(2)
expect(actual[0]).toMatchObject({ el: 'div.inner-container', left: 0, top: 100 })
expect(actual).toMatchSnapshot()
})

test('block: "start"', async () => {
expect.assertions(3)
const windowHeight = await page.evaluate(() => window.innerHeight);
const actual = await page.evaluate(() => {
return window
.computeScrollIntoView(document.querySelector('.target'), {
block: 'start',
})
.map(window.mapActions)
})
expect(actual).toHaveLength(2)
expect(actual[0]).toMatchObject({ el: 'div.inner-container', left: 0, top: windowHeight })
expect(actual).toMatchSnapshot()
})
})
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ const alignNearest = (
}

const getParentElement = (element: Node): Element | null => {
const parent = element.parentElement
const parent = isElement(element)
? (element.assignedSlot ?? element.parentElement)
: element.parentElement
if (parent == null) {
return (element.getRootNode() as ShadowRoot).host || null
}
Expand Down