Sure! Below is an implementation of a BottomSheet class in JavaScript with a sleek dark Material You design. This includes the JavaScript class, the CSS styles, and an example of how to use it.
JavaScript (BottomSheet class):
class BottomSheet {
constructor() {
// Create the overlay
this.overlay = document.createElement('div');
this.overlay.classList.add('bottom-sheet-overlay');
// Create the bottom sheet container
this.sheet = document.createElement('div');
this.sheet.classList.add('bottom-sheet');
// Create the content container
this.content = document.createElement('div');
this.content.classList.add('bottom-sheet-content');
this.sheet.appendChild(this.content);
document.body.appendChild(this.overlay);
document.body.appendChild(this.sheet);
// Event listeners for closing the bottom sheet
this.overlay.addEventListener('click', () => this.hide());
// Options
this._options = [];
}
set options(opts) {
this._options = opts;
this.renderOptions();
}
renderOptions() {
// Clear existing content
this.content.innerHTML = '';
// Create options elements
this._options.forEach(option => {
const optionElement = document.createElement('div');
optionElement.classList.add('bottom-sheet-option');
optionElement.textContent = option.text;
optionElement.addEventListener('click', () => {
if (option.onclick) option.onclick();
this.hide();
});
this.content.appendChild(optionElement);
});
}
show() {
this.overlay.classList.add('visible');
this.sheet.classList.add('visible');
}
hide() {
this.overlay.classList.remove('visible');
this.sheet.classList.remove('visible');
}
}
CSS (Sleek Dark Material You Design):
/* Overlay */
.bottom-sheet-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(18, 18, 18, 0.6); /* Semi-transparent dark overlay */
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
z-index: 999;
}
.bottom-sheet-overlay.visible {
opacity: 1;
pointer-events: all;
}
/* Bottom Sheet Container */
.bottom-sheet {
position: fixed;
bottom: -100%;
left: 0;
width: 100%;
background-color: #121212; /* Dark background */
border-top-left-radius: 16px;
border-top-right-radius: 16px;
box-shadow: 0 -4px 8px rgba(0, 0, 0, 0.5);
transition: bottom 0.3s ease;
z-index: 1000;
}
/* Visible state */
.bottom-sheet.visible {
bottom: 0;
}
/* Content */
.bottom-sheet-content {
padding: 16px;
}
/* Option Items */
.bottom-sheet-option {
padding: 16px;
color: #E0E0E0; /* Light text color */
font-size: 16px;
font-weight: 500;
cursor: pointer;
border-radius: 8px;
transition: background-color 0.2s ease;
}
.bottom-sheet-option + .bottom-sheet-option {
margin-top: 8px;
}
.bottom-sheet-option:hover {
background-color: rgba(255, 255, 255, 0.1);
}
Usage Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bottom Sheet Example</title>
<!-- Include the CSS -->
<style>
/* Paste the CSS code from above here */
</style>
</head>
<body>
<button id="show-bottom-sheet">Show Bottom Sheet</button>
<!-- Include the JavaScript -->
<script>
// Paste the JavaScript code from above here
// Example usage
const bottomSheet = new BottomSheet();
bottomSheet.options = [
{
text: 'Rename',
onclick: () => {
alert('Rename clicked');
}
},
{
text: 'Delete',
onclick: () => {
alert('Delete clicked');
}
},
{
text: 'Share',
onclick: () => {
alert('Share clicked');
}
},
{
text: 'Add to Favorites',
onclick: () => {
alert('Add to Favorites clicked');
}
}
];
document.getElementById('show-bottom-sheet').addEventListener('click', () => {
bottomSheet.show();
});
</script>
</body>
</html>
Explanation:
Notes:
- This implementation uses vanilla JavaScript and CSS, so it doesn't require any external libraries.
- You can adjust the styles and options as needed to better match your specific use case or design preferences.
- Ensure that the CSS is included in your project either in a
<style> tag or an external stylesheet.
- The
BottomSheet can be enhanced further by adding features like drag-to-dismiss, dynamic content, or integrating with a JavaScript framework if needed.
Enjoy your sleek dark Material You bottom sheet!