Skip to content
Draft
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
4 changes: 4 additions & 0 deletions all.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import './buttons/button.js'
import './buttons/filled-tonal-button.js'
import './buttons/outlined-button.js'
import './buttons/text-button.js'
import './carousel/carousel.js'
import './carousel/carousel-item.js'
import './checkbox/checkbox.js'
import './chips/assist-chip.js'
import './chips/chip-set.js'
Expand Down Expand Up @@ -48,6 +50,8 @@ import './text/text-field.js'
// LINT.IfChange(exports)
// go/keep-sorted start
export * from './buttons/button.js'
export * from './carousel/carousel.js'
export * from './carousel/carousel-item.js'
export * from './checkbox/checkbox.js'
export * from './chips/chip.js'
export * from './chips/chip-set.js'
Expand Down
44 changes: 44 additions & 0 deletions carousel/carousel-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { html, LitElement, css } from 'lit';

/**
* An item inside a Material 3 Carousel.
*
* @element md-carousel-item
*/
export class CarouselItem extends LitElement {
static get styles() {
return [
css`
:host {
display: block;
border-radius: 16px;
overflow: hidden;
/* Default aspect ratio, can be overridden */
aspect-ratio: 1;
width: 200px; /* Default width, can be overridden */
scroll-snap-align: start;
flex: 0 0 auto;
}
Comment on lines +12 to +21
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider using CSS custom properties for dimensions and styles like border-radius, aspect-ratio, and width. This allows consumers to easily customize the carousel items to fit different Material 3 carousel types (e.g., hero, multi-browse, uncontained) without needing to override the entire component's styles.

Suggested change
:host {
display: block;
border-radius: 16px;
overflow: hidden;
/* Default aspect ratio, can be overridden */
aspect-ratio: 1;
width: 200px; /* Default width, can be overridden */
scroll-snap-align: start;
flex: 0 0 auto;
}
:host {
display: block;
border-radius: var(--md-carousel-item-container-shape, 16px);
overflow: hidden;
/* Default aspect ratio, can be overridden */
aspect-ratio: var(--md-carousel-item-aspect-ratio, 1);
width: var(--md-carousel-item-width, 200px); /* Default width, can be overridden */
scroll-snap-align: start;
flex: 0 0 auto;
}


::slotted(img) {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
`
];
}

constructor() {
super();
this.internals = this.attachInternals();
this.internals.role = 'listitem';
}

render() {
return html`<slot></slot>`;
}
}

customElements.define('md-carousel-item', CarouselItem);
45 changes: 45 additions & 0 deletions carousel/carousel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { html, LitElement, css } from 'lit';

/**
* A Material 3 Carousel component.
*
* @element md-carousel
*/
export class Carousel extends LitElement {
static get styles() {
return [
css`
:host {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
gap: 8px;
padding: 16px;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE/Edge */
}
Comment on lines +12 to +20
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When using scroll-snap-type with container padding, it is important to set scroll-padding to match. This ensures that items snap correctly relative to the padding rather than the edge of the scroll container. Additionally, using CSS custom properties for spacing improves themeability and adherence to Material Design principles.

Suggested change
:host {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
gap: 8px;
padding: 16px;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE/Edge */
}
:host {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-padding: var(--md-carousel-container-padding, 16px);
gap: var(--md-carousel-container-gap, 8px);
padding: var(--md-carousel-container-padding, 16px);
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE/Edge */
}


:host::-webkit-scrollbar {
display: none; /* Chrome/Safari/Opera */
}

::slotted(*) {
scroll-snap-align: start;
flex: 0 0 auto;
}
`
];
}

constructor() {
super();
this.internals = this.attachInternals();
this.internals.role = 'list';
}
Comment on lines +34 to +38
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The carousel container should be focusable to support keyboard navigation. Since scrollbars are hidden (lines 18-24), keyboard users rely on arrow keys to scroll the content, which requires the scrollable element to be focusable via tabindex.

Suggested change
constructor() {
super();
this.internals = this.attachInternals();
this.internals.role = 'list';
}
constructor() {
super();
this.internals = this.attachInternals();
this.internals.role = 'list';
this.tabIndex = 0;
}


render() {
return html`<slot></slot>`;
}
}

customElements.define('md-carousel', Carousel);
4 changes: 4 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* for production.
*/
import './buttons/button.js'
import './carousel/carousel.js'
import './carousel/carousel-item.js'
import './checkbox/checkbox.js'
import './chips/chip.js'
import './chips/chip-set.js'
Expand All @@ -27,6 +29,8 @@ import './tabs/tabs.js'
import './text/text-field.js'

export * from './buttons/button.js'
export * from './carousel/carousel.js'
export * from './carousel/carousel-item.js'
export * from './checkbox/checkbox.js'
export * from './chips/chip.js'
export * from './chips/chip-set.js'
Expand Down
83 changes: 83 additions & 0 deletions demo/carousel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Material 3 Carousel Demo</title>

<style>
body {
font-family: sans-serif;
padding: 24px;
margin: 0;
background-color: #f5f5f5;
}

h1 {
margin-bottom: 24px;
}

.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 24px;
border-radius: 16px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

md-carousel {
border: 1px solid #e0e0e0;
border-radius: 16px;
background-color: #fafafa;
}
</style>

<script type="importmap">
{
"imports": {
"lit": "https://cdn.jsdelivr.net/npm/lit@3/index.js",
"lit/": "https://cdn.jsdelivr.net/npm/lit@3/",
"@lit/localize": "https://cdn.jsdelivr.net/npm/@lit/localize/lit-localize.js",
"@lit/reactive-element": "https://cdn.jsdelivr.net/npm/@lit/reactive-element@1/reactive-element.js",
"@lit/reactive-element/": "https://cdn.jsdelivr.net/npm/@lit/reactive-element@1/",
"lit-element/lit-element.js": "https://cdn.jsdelivr.net/npm/lit-element@4/lit-element.js",
"lit-html": "https://cdn.jsdelivr.net/npm/lit-html@3/lit-html.js",
"lit-html/": "https://cdn.jsdelivr.net/npm/lit-html@3/",
"material/": "../"
}
}
</script>

<script type="module">
import 'material/carousel/carousel.js';
import 'material/carousel/carousel-item.js';
</script>
</head>
<body>
<div class="container">
<h1>Material 3 Carousel</h1>

<md-carousel>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=1" alt="Random image 1">
</md-carousel-item>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=2" alt="Random image 2">
</md-carousel-item>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=3" alt="Random image 3">
</md-carousel-item>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=4" alt="Random image 4">
</md-carousel-item>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=5" alt="Random image 5">
</md-carousel-item>
<md-carousel-item>
<img src="https://picsum.photos/400/400?random=6" alt="Random image 6">
</md-carousel-item>
</md-carousel>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<div>
Other demo pages:
<ul>
<li><a href="./carousel.html">Carousel Demo</a></li>
<li><a href="./list-demo.html">List Demo</a></li>
</ul>

Expand Down