1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-13 17:44:19 +02:00

feat: Custom checkbox button

This commit is contained in:
Phuoc Nguyen
2022-09-21 09:09:00 +07:00
parent 37a6729d08
commit 14e1965796
8 changed files with 116 additions and 258 deletions

View File

@@ -0,0 +1,9 @@
{% for i in range(0, 3) %}
<label class="custom-checkbox-button">
<input type="checkbox" class="custom-checkbox-button__input" />
<div class="custom-checkbox-button__square">
<div class="custom-checkbox-button__checkbox custom-checkbox-button__checkbox--selected"></div>
</div>
{% rectangle %}
</label>
{% endfor %}

View File

@@ -0,0 +1,65 @@
---
layout: layouts/post.njk
title: Custom checkbox button
description: Create a custom checkbox button with CSS flexbox
keywords: css checkbox, css flexbox
---
## HTML
```html
<label class="custom-checkbox-button">
<!-- The real checkbox -->
<input type="checkbox" class="custom-checkbox-button__input" />
<!-- The fake square -->
<div class="custom-checkbox-button__square">
<!-- The inner square -->
<div class="custom-checkbox-button__checkbox custom-checkbox-button__checkbox--selected"></div>
</div>
<!-- The text -->
...
</label>
```
## CSS
```css
.custom-checkbox-button {
/* Center the content horizontally */
align-items: center;
display: inline-flex;
/* Cursor */
cursor: pointer;
}
.custom-checkbox-button__input {
/* Hide it */
display: none;
}
.custom-checkbox-button__square {
border: 1px solid #d1d5db;
border-radius: 0.25rem;
/* Spacing */
margin-right: 0.5rem;
padding: 0.25rem;
}
.custom-checkbox-button__checkbox {
background-color: transparent;
border-radius: 0.25rem;
height: 1rem;
width: 1rem;
}
.custom-checkbox-button__checkbox--selected {
/* For selected checkbox */
background-color: #3b82f6;
}
```
{% demo %}{% include "patterns/custom-checkbox-button.njk" %}{% enddemo %}

View File

@@ -89,6 +89,7 @@ eleventyExcludeFromCollections: true
<div class="category__posts">
{% pattern "Button with icon" %}{% include "patterns/button-with-icon.njk" %}{% endpattern %}
{% pattern "Chip" %}{% include "patterns/chip.njk" %}{% endpattern %}
{% pattern "Custom checkbox button" %}{% include "patterns/custom-checkbox-button.njk" %}{% endpattern %}
</div>
</div>

View File

@@ -1,179 +0,0 @@
import * as React from 'react';
import Head from 'next/head';
import { Spacer } from '@1milligram/design';
import { RelatedPatterns } from '../../components/RelatedPatterns';
import { Pattern } from '../../constants/Pattern';
import { PatternLayout } from '../../layouts/PatternLayout';
import BrowserFrame from '../../placeholders/BrowserFrame';
import Rectangle from '../../placeholders/Rectangle';
interface CheckboxProps {
isChecked: boolean;
value: string;
}
const Details: React.FC<{}> = () => {
const Checkbox: React.FC<CheckboxProps> = ({ isChecked, value, children }) => {
const [checked, setChecked] = React.useState(isChecked);
const click = () => setChecked((c) => !c);
return (
<label
htmlFor="custom-checkbox-button"
style={{
alignItems: 'center',
cursor: 'pointer',
display: 'inline-flex',
}}
>
<input
id="custom-checkbox-button"
type="checkbox"
name="option"
value={value}
checked={checked}
style={{ display: 'none' }}
onChange={click}
/>
<div
style={{
border: '1px solid #d1d5db',
borderRadius: '4px',
marginRight: '8px',
padding: '4px',
}}
>
<div
style={{
backgroundColor: checked ? '#00449E' : 'transparent',
borderRadius: '4px',
height: '16px',
width: '16px',
}}
/>
</div>
{children}
</label>
);
};
return (
<PatternLayout pattern={Pattern.CustomCheckboxButton}>
<Head>
<meta name="description" content="Create a custom checkbox button with CSS flexbox" />
<meta name="og:description" content="Create a custom checkbox button with CSS flexbox" />
<meta name="twitter:description" content="Create a custom checkbox button with CSS flexbox" />
<meta name="keywords" content="css checkbox, css flexbox" />
</Head>
<BrowserFrame
html={`
<label class="label">
<!-- The real checkbox -->
<input type="checkbox" class="label__input" />
<!-- The fake square -->
<div class="label__square">
<!-- The inner square -->
<div class="label__checkbox label__square--selected"></div>
</div>
<!-- The text -->
...
</div>
`}
css={`
.label {
/* Center the content horizontally */
align-items: center;
display: inline-flex;
/* Cursor */
cursor: pointer;
}
.label__input {
/* Hide it */
display: none;
}
.label__square {
border: 1px solid #d1d5db;
border-radius: 4px;
/* Spacing */
margin-right: 8px;
padding: 4px;
}
.label__checkbox {
background-color: transparent;
border-radius: 4px;
height: 16px;
width: 16px;
}
.label__checkbox--selected {
/* For selected checkbox */
background-color: #00449e;
}
`}
>
<div
style={{
alignItems: 'center',
display: 'flex',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div style={{ width: '200px' }}>
<div
style={{
alignItems: 'center',
display: 'inline-flex',
marginBottom: '16px',
}}
>
<Checkbox value="1" isChecked={false}>
<div style={{ width: '100px' }}>
<Rectangle />
</div>
</Checkbox>
</div>
<div
style={{
alignItems: 'center',
display: 'inline-flex',
marginBottom: '16px',
}}
>
<Checkbox value="2" isChecked={true}>
<div style={{ width: '200px' }}>
<Rectangle />
</div>
</Checkbox>
</div>
<div
style={{
alignItems: 'center',
display: 'inline-flex',
}}
>
<Checkbox value="3" isChecked={false}>
<div style={{ width: '150px' }}>
<Rectangle />
</div>
</Checkbox>
</div>
</div>
</div>
</BrowserFrame>
<Spacer size="extraLarge" />
<RelatedPatterns patterns={[Pattern.CustomRadioButton, Pattern.RadioButtonGroup]} />
</PatternLayout>
);
};
export default Details;

View File

@@ -1,79 +0,0 @@
import * as React from 'react';
import Frame from '../../placeholders/Frame';
import Rectangle from '../../placeholders/Rectangle';
import Square from '../../placeholders/Square';
const Cover: React.FC<{}> = () => {
return (
<Frame>
<div
style={{
alignItems: 'center',
display: 'flex',
flexDirection: 'column',
height: '100%',
justifyContent: 'center',
padding: '8px',
}}
>
<div
style={{
alignItems: 'center',
display: 'flex',
marginBottom: '8px',
width: '100%',
}}
>
<div
style={{
alignItems: 'center',
border: '1px solid #d1d5db',
borderRadius: '4px',
display: 'flex',
height: '24px',
justifyContent: 'center',
marginRight: '8px',
padding: '4px',
width: '24px',
}}
>
<Square backgroundColor="transparent" />
</div>
<div style={{ flex: 1 }}>
<Rectangle />
</div>
</div>
<div
style={{
alignItems: 'center',
display: 'flex',
marginBottom: '8px',
width: '100%',
}}
>
<div
style={{
alignItems: 'center',
border: '1px solid #d1d5db',
borderRadius: '4px',
display: 'flex',
height: '24px',
justifyContent: 'center',
marginRight: '8px',
padding: '4px',
width: '24px',
}}
>
<Square backgroundColor="#00449E" />
</div>
<div style={{ flex: 1 }}>
<Rectangle />
</div>
</div>
</div>
</Frame>
);
};
export default Cover;

View File

@@ -16,6 +16,8 @@
flex-direction: column;
justify-content: center;
pointer-events: none;
height: 8rem;
width: 100%;
margin-bottom: 0.5rem;

View File

@@ -26,6 +26,7 @@
@import './patterns/cookie-banner';
@import './patterns/corner-ribbon';
@import './patterns/curved-background';
@import './patterns/custom-checkbox-button';
@import './patterns/diagonal-section';
@import './patterns/docked-at-corner';
@import './patterns/dot-leader';

View File

@@ -0,0 +1,38 @@
.custom-checkbox-button {
/* Center the content horizontally */
align-items: center;
display: inline-flex;
/* Cursor */
cursor: pointer;
/* Demo */
margin: 0.25rem 0;
width: 8rem;
}
.custom-checkbox-button__input {
/* Hide it */
display: none;
}
.custom-checkbox-button__square {
border: 1px solid #d1d5db;
border-radius: 0.25rem;
/* Spacing */
margin-right: 0.5rem;
padding: 0.25rem;
}
.custom-checkbox-button__checkbox {
background-color: transparent;
border-radius: 0.25rem;
height: 1rem;
width: 1rem;
}
.custom-checkbox-button__checkbox--selected {
/* For selected checkbox */
background-color: #3b82f6;
}