Skip to content

Instantly share code, notes, and snippets.

@tsmd
Created May 29, 2019 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsmd/e5e7b51968d6f992ec4032757a643777 to your computer and use it in GitHub Desktop.
Save tsmd/e5e7b51968d6f992ec4032757a643777 to your computer and use it in GitHub Desktop.
Cool checkbox
@charset "UTF-8";
.Checkbox {
@at-root {
@function checked-icon($color: #fff) {
$color: '' + $color;
$color: '%23' + str-slice($color, 2);
@return 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M9%2016.17L4.83%2012l-1.42%201.41L9%2019%2021%207l-1.41-1.41z%22%20fill%3D%22#{$color}%22%2F%3E%3C%2Fsvg%3E';
}
& {
position: relative;
z-index: 0;
display: inline-flex;
justify-content: flex-start;
align-items: baseline;
line-height: 1.8;
}
.Checkbox__input {
position: absolute;
z-index: -1;
opacity: 0;
}
.Checkbox__icon {
flex: 0 0 auto;
position: relative;
top: 0.1em; // vertical-align としての役割
width: 1em;
height: 1em;
border-radius: 4px;
background-color: $color-gray30;
// クリック範囲拡張
&::before {
content: '';
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
&::after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: none;
width: (12px / 16px) * 100%;
height: (12px / 16px) * 100%;
margin: auto;
background: url(checked-icon(#fff)) no-repeat center;
}
}
.Checkbox__label {
margin-left: (6px / 16px) * 1em;
}
// フォーカス
:focus.focus-visible ~ .Checkbox__icon {
box-shadow: 0 0 0 4px rgba($color-primary, 0.5);
}
// チェック済み
:checked ~ .Checkbox__icon {
background-color: $color-primary;
&::after {
display: block;
}
}
// 中間状態
:indeterminate ~ .Checkbox__icon {
background-color: $color-primary-light;
&::after {
display: block;
width: (6px / 16px) * 100%;
height: (2px / 16px) * 100%;
background: #fff;
}
}
// 使用不可
:disabled {
& ~ .Checkbox__icon {
background-color: $color-gray10;
&::after {
background-image: url(checked-icon(#8ca0a5));
}
}
& ~ .Checkbox__label {
color: $color-gray60;
}
}
}
}
<!DOCTYPE html>
<html lang="ja">
<title>Checkbox</title>
<label class="Checkbox">
<input class="Checkbox__input" type="checkbox" aria-label="テキストラベル">
<span class="Checkbox__icon"></span>
</label>
<label class="Checkbox">
<input class="Checkbox__input" type="checkbox">
<span class="Checkbox__icon"></span>
<span class="Checkbox__label">
テキストラベル
</span>
</label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment