Skip to content

Commit 200a3c2

Browse files
committed
Add more CSS variables for customization. Update README
1 parent c074707 commit 200a3c2

File tree

3 files changed

+106
-75
lines changed

3 files changed

+106
-75
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ HTML Custom Element for creating sheets. Displayed as a bottom sheet on mobile a
55

66
## Features
77

8-
- Has a draggable area to resize the sheet
8+
- There is a draggable area to open or close the sheet
99
- The sheet can be closed using a button in the top right corner, using the `Esc` key, or by clicking outside the bottom sheet
1010
- This behavior is configurable. You can turn off the `Esc` or the click outside the sheet when you want.
1111
- API is similar to the `<dialog>` element's
12+
- Supports forms inside of it
13+
- Uses familiar method names and the same event names
14+
- There are many customization options
1215

1316

1417
## Installation

library/sheet.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class SheetElement extends HTMLElement {
8383
* Gray area on the top of the sheet to resize the sheet
8484
* @type {HTMLElement}
8585
*/
86-
#draggableArea
86+
#handle
8787

8888
#scaleDownTo
8989

@@ -175,12 +175,12 @@ export class SheetElement extends HTMLElement {
175175
</div>
176176

177177
<div
178-
class="sheet-draggable-area"
179-
reference={area => this.#draggableArea = area}
178+
class="sheet-handle-container"
179+
reference={area => this.#handle = area}
180180
onMouseDown={this.#eventListeners.onDragStart}
181181
onTouchStart={this.#eventListeners.onDragStart}
182182
>
183-
<div class="sheet-draggable-thumb"></div>
183+
<div class="sheet-handle"></div>
184184
</div>
185185

186186
<div class="sheet-button-area">
@@ -365,7 +365,7 @@ export class SheetElement extends HTMLElement {
365365
#onDragStart(event) {
366366
this.#dragPosition = touchPosition(event).pageY
367367
this.#sheet.classList.add("is-resized")
368-
this.#draggableArea.style.cursor = document.body.style.cursor = "grabbing"
368+
this.#handle.style.cursor = document.body.style.cursor = "grabbing"
369369

370370
this.#scaleDownTo = +getCSSVariableValue(this.#sheet, "--scale-down-to")
371371
}
@@ -406,7 +406,7 @@ export class SheetElement extends HTMLElement {
406406
this.close()
407407
}
408408

409-
this.#draggableArea.style.cursor = document.body.style.cursor = ""
409+
this.#handle.style.cursor = document.body.style.cursor = ""
410410
this.#dragPosition = undefined
411411

412412
this.#sheet.classList.remove("is-resized")

library/styleSheet.js

Lines changed: 96 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,70 @@ export default styleSheet
88

99
styleSheet.replaceSync(`
1010
:host {
11-
--overlay-color: #88888880;
12-
--sheet-background-color: #fff;
13-
--sheet-thumb-color: #eee;
14-
--sheet-transition-duration: 0.5s;
15-
--sheet-border-radius: 1rem;
16-
--sheet-header-padding: 0 0 0 1rem;
17-
--sheet-body-padding: 1rem;
11+
--_sheet-foreground-color: var(--sheet-foreground-color, inherit);
12+
--_sheet-background-color: var(--sheet-background-color, #fff);
1813
14+
--_sheet-border-radius: var(--sheet-border-radius, 1rem);
15+
16+
--_sheet-min-width: var(--sheet-min-width, 18rem);
17+
--_sheet-width: var(--sheet-width, 90vw);
18+
--_sheet-max-width: var(--sheet-max-width, auto);
19+
20+
--_sheet-min-height: var(--sheet-min-height, 30vh);
21+
--_sheet-height: var(--sheet-height, auto);
22+
--_sheet-max-height: var(--sheet-max-height, 100vh);
23+
24+
--_sheet-scale-down-to: 0.5;
25+
--_sheet-z-index: var(--sheet-z-index, 1);
26+
--_sheet-transition-duration: var(--sheet-transition-duration, 0.5s);
27+
28+
--_sheet-backdrop-color: var(--sheet-backdrop-color, #88888880);
29+
30+
--_sheet-header-padding: var(--sheet-header-padding, 0 0 0 1rem);
31+
--_sheet-body-padding: var(--sheet-body-padding, 1rem);
32+
33+
--_sheet-handle-width: var(--sheet-handle-width, 3rem);
34+
--_sheet-handle-height: var(--sheet-handle-height, 0.25rem);
35+
--_sheet-handle-color: var(--sheet-handle-color, #eee);
36+
--_sheet-handle-border-radius: var(--sheet-handle-border-radius, 0.125rem);
37+
--_sheet-handle-container-padding: var(--sheet-handle-container-padding, 1rem);
38+
}
39+
40+
@media (prefers-color-scheme: dark) {
41+
:host {
42+
--_sheet-background-color: var(--sheet-background-color, black);
43+
--_sheet-foreground-color: var(--sheet-foreground-color, white);
44+
--_sheet-handle-color: var(--sheet-handle-color, #333333);
45+
}
46+
}
47+
48+
@media (prefers-reduced-motion: reduce) {
49+
:host {
50+
--_sheet-transition-duration: var(--sheet-transition-duration, 0.1s);
51+
}
52+
}
53+
54+
/* tablet */
55+
@media (min-width: 48rem) {
56+
:host {
57+
--_sheet-width: var(--sheet-width, auto);
58+
--_sheet-max-width: 48rem;
59+
--_sheet-max-height: 32rem;
60+
61+
border-radius: var(--_sheet-border-radius);
62+
justify-content: center;
63+
}
64+
65+
.sheet-handle-container {
66+
display: none;
67+
}
68+
69+
.sheet-controls {
70+
grid-template-columns: 1fr auto auto;
71+
}
72+
}
73+
74+
:host {
1975
display: flex;
2076
flex-direction: column;
2177
align-items: center;
@@ -26,11 +82,11 @@ styleSheet.replaceSync(`
2682
left: 0;
2783
right: 0;
2884
bottom: 0;
29-
z-index: 2;
85+
z-index: var(--_sheet-z-index);
3086
3187
transition:
32-
opacity var(--sheet-transition-duration),
33-
visibility var(--sheet-transition-duration);
88+
opacity var(--_sheet-transition-duration),
89+
visibility var(--_sheet-transition-duration);
3490
}
3591
3692
:host(:not([open])) {
@@ -39,59 +95,45 @@ styleSheet.replaceSync(`
3995
pointer-events: none;
4096
}
4197
42-
@media (prefers-color-scheme: dark) {
43-
:host {
44-
--sheet-background-color: black;
45-
--sheet-foreground-color: white;
46-
--sheet-thumb-color: #333333;
47-
}
48-
}
49-
50-
@media (prefers-reduced-motion: reduce) {
51-
:host {
52-
--sheet-transition-duration: 0.1s;
53-
}
54-
}
55-
5698
/* ::backdrop is not supported :( */
5799
.sheet-backdrop {
58100
position: absolute;
59101
top: 0;
60102
left: 0;
61103
right: 0;
62104
bottom: 0;
63-
background-color: var(--overlay-color);
105+
background-color: var(--_sheet-backdrop-color);
64106
}
65107
66108
.sheet-contents {
67109
display: flex;
68110
flex-direction: column;
69111
70-
border-radius: var(--sheet-border-radius) var(--sheet-border-radius) 0 0;
112+
border-radius: var(--_sheet-border-radius) var(--_sheet-border-radius) 0 0;
71113
72-
background: var(--sheet-background-color);
114+
background: var(--_sheet-background-color);
73115
74116
overflow-y: hidden;
75117
76118
transform: translateY(0) scale(1);
77119
78-
min-width: 18rem;
79-
width: 90vw;
120+
min-width: var(--_sheet-min-width);
121+
width: var(--_sheet-width);
122+
max-width: var(--_sheet-max-width);
80123
81-
min-height: 30vh;
82-
max-height: 100vh;
124+
min-height: var(--_sheet-min-height);
125+
height: var(--_sheet-height);
126+
max-height: var(--_sheet-max-height);
83127
84128
box-sizing: border-box;
85129
86-
--scale-down-to: 0.5;
87-
88130
transition:
89-
transform var(--sheet-transition-duration),
90-
border-radius var(--sheet-transition-duration);
131+
transform var(--_sheet-transition-duration),
132+
border-radius var(--_sheet-transition-duration);
91133
}
92134
93135
:host(:not([open])) .sheet-contents {
94-
transform: translateY(100%) scale(var(--scale-down-to));
136+
transform: translateY(100%) scale(var(--_sheet-scale-down-to));
95137
}
96138
97139
.sheet-contents.is-resized {
@@ -102,26 +144,34 @@ styleSheet.replaceSync(`
102144
display: grid;
103145
grid-template-columns: 1fr auto 1fr;
104146
align-items: stretch;
105-
padding: var(--sheet-header-padding);
147+
padding: var(--_sheet-header-padding);
106148
}
107149
108150
.sheet-title-area {
109151
display: flex;
110152
justify-content: flex-start;
111153
}
112154
113-
.sheet-draggable-area {
114-
width: 3rem;
155+
.sheet-handle-container {
156+
display: flex;
157+
flex-direction: column;
158+
justify-content: center;
159+
160+
min-height: var(--_sheet-handle-height);
161+
height: 100%;
162+
163+
padding: var(--_sheet-handle-container-padding);
164+
box-sizing: border-box;
165+
115166
margin: auto;
116-
padding: 1rem;
117167
cursor: grab;
118168
}
119169
120-
.sheet-draggable-thumb {
121-
width:inherit;
122-
height: 0.25rem;
123-
background: var(--sheet-thumb-color);
124-
border-radius: 0.125rem;
170+
.sheet-handle {
171+
width: var(--_sheet-handle-width);
172+
height: var(--_sheet-handle-height);
173+
background: var(--_sheet-handle-color);
174+
border-radius: var(--_sheet-handle-border-radius);
125175
}
126176
127177
.sheet-button-area {
@@ -147,29 +197,7 @@ styleSheet.replaceSync(`
147197
148198
overflow-y: auto;
149199
150-
padding: var(--sheet-body-padding);
200+
padding: var(--_sheet-body-padding);
151201
box-sizing: border-box;
152202
}
153-
154-
/* tablet */
155-
@media (min-width: 48rem) {
156-
:host {
157-
justify-content: center;
158-
}
159-
160-
.sheet-contents {
161-
width: auto;
162-
max-width: 48rem;
163-
max-height: 32rem;
164-
border-radius: var(--sheet-border-radius);
165-
}
166-
167-
.sheet-draggable-area {
168-
display: none;
169-
}
170-
171-
.sheet-controls {
172-
grid-template-columns: 1fr auto auto;
173-
}
174-
}
175203
`)

0 commit comments

Comments
 (0)