Skip to content

Commit 01f9dc5

Browse files
authored
refactor: extract checkbox styles to reusable css literal (#5562)
1 parent c2bd6a8 commit 01f9dc5

4 files changed

Lines changed: 72 additions & 53 deletions

File tree

‎packages/checkbox/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"@vaadin/field-base": "24.0.0-beta1",
4141
"@vaadin/vaadin-lumo-styles": "24.0.0-beta1",
4242
"@vaadin/vaadin-material-styles": "24.0.0-beta1",
43-
"@vaadin/vaadin-themable-mixin": "24.0.0-beta1"
43+
"@vaadin/vaadin-themable-mixin": "24.0.0-beta1",
44+
"lit": "^2.0.0"
4445
},
4546
"devDependencies": {
4647
"@esm-bundle/chai": "^4.3.4",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import type { CSSResult } from 'lit';
7+
8+
export const checkboxStyles: CSSResult;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
7+
8+
export const checkboxStyles = css`
9+
:host {
10+
display: inline-block;
11+
}
12+
13+
:host([hidden]) {
14+
display: none !important;
15+
}
16+
17+
:host([disabled]) {
18+
-webkit-tap-highlight-color: transparent;
19+
}
20+
21+
.vaadin-checkbox-container {
22+
display: grid;
23+
grid-template-columns: auto 1fr;
24+
align-items: baseline;
25+
}
26+
27+
[part='checkbox'],
28+
::slotted(input),
29+
::slotted(label) {
30+
grid-row: 1;
31+
}
32+
33+
[part='checkbox'],
34+
::slotted(input) {
35+
grid-column: 1;
36+
}
37+
38+
[part='checkbox'] {
39+
width: var(--vaadin-checkbox-size, 1em);
40+
height: var(--vaadin-checkbox-size, 1em);
41+
}
42+
43+
[part='checkbox']::before {
44+
display: block;
45+
content: '\\202F';
46+
line-height: var(--vaadin-checkbox-size, 1em);
47+
contain: paint;
48+
}
49+
50+
/* visually hidden */
51+
::slotted(input) {
52+
opacity: 0;
53+
cursor: inherit;
54+
margin: 0;
55+
align-self: stretch;
56+
-webkit-appearance: none;
57+
}
58+
`;

‎packages/checkbox/src/vaadin-checkbox.js‎

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { CheckedMixin } from '@vaadin/field-base/src/checked-mixin.js';
1313
import { InputController } from '@vaadin/field-base/src/input-controller.js';
1414
import { LabelMixin } from '@vaadin/field-base/src/label-mixin.js';
1515
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
16-
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
16+
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
17+
import { checkboxStyles } from './vaadin-checkbox-styles.js';
18+
19+
registerStyles('vaadin-checkbox', checkboxStyles, { moduleId: 'vaadin-checkbox-styles' });
1720

1821
/**
1922
* `<vaadin-checkbox>` is an input field representing a binary choice.
@@ -65,57 +68,6 @@ class Checkbox extends LabelMixin(
6568

6669
static get template() {
6770
return html`
68-
<style>
69-
:host {
70-
display: inline-block;
71-
}
72-
73-
:host([hidden]) {
74-
display: none !important;
75-
}
76-
77-
:host([disabled]) {
78-
-webkit-tap-highlight-color: transparent;
79-
}
80-
81-
.vaadin-checkbox-container {
82-
display: grid;
83-
grid-template-columns: auto 1fr;
84-
align-items: baseline;
85-
}
86-
87-
[part='checkbox'],
88-
::slotted(input),
89-
::slotted(label) {
90-
grid-row: 1;
91-
}
92-
93-
[part='checkbox'],
94-
::slotted(input) {
95-
grid-column: 1;
96-
}
97-
98-
[part='checkbox'] {
99-
width: var(--vaadin-checkbox-size, 1em);
100-
height: var(--vaadin-checkbox-size, 1em);
101-
}
102-
103-
[part='checkbox']::before {
104-
display: block;
105-
content: '\\202F';
106-
line-height: var(--vaadin-checkbox-size, 1em);
107-
contain: paint;
108-
}
109-
110-
/* visually hidden */
111-
::slotted(input) {
112-
opacity: 0;
113-
cursor: inherit;
114-
margin: 0;
115-
align-self: stretch;
116-
-webkit-appearance: none;
117-
}
118-
</style>
11971
<div class="vaadin-checkbox-container">
12072
<div part="checkbox" aria-hidden="true"></div>
12173
<slot name="input"></slot>

0 commit comments

Comments
 (0)