1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-05 21:57:32 +02:00

feat: Custom radio button

This commit is contained in:
Phuoc Nguyen
2022-09-21 09:16:07 +07:00
parent 14e1965796
commit 203c6f299c
7 changed files with 122 additions and 257 deletions

View File

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

View File

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

View File

@@ -90,6 +90,7 @@ eleventyExcludeFromCollections: true
{% 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 %}
{% pattern "Custom radio button" %}{% include "patterns/custom-radio-button.njk" %}{% endpattern %}
</div>
</div>

View File

@@ -1,182 +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 RadioProps {
value: string;
}
const Details: React.FC<{}> = () => {
const [selectedValue, setSelectedValue] = React.useState('1');
const Radio: React.FC<RadioProps> = ({ value, children }) => {
const click = () => setSelectedValue(value);
return (
<label
htmlFor="custom-radio-button"
style={{
alignItems: 'center',
cursor: 'pointer',
display: 'inline-flex',
}}
>
<input
id="custom-radio-button"
type="radio"
name="option"
value={value}
style={{ display: 'none' }}
onChange={click}
/>
<div
style={{
border: '1px solid #d1d5db',
borderRadius: '9999px',
marginRight: '8px',
padding: '4px',
}}
>
<div
style={{
backgroundColor: value === selectedValue ? '#00449E' : 'transparent',
borderRadius: '9999px',
height: '16px',
width: '16px',
}}
/>
</div>
{children}
</label>
);
};
return (
<PatternLayout pattern={Pattern.CustomRadioButton}>
<Head>
<meta name="description" content="Create a custom radio button with CSS flexbox" />
<meta name="og:description" content="Create a custom radio button with CSS flexbox" />
<meta name="twitter:description" content="Create a custom radio button with CSS flexbox" />
<meta name="keywords" content="css flexbox, css radio" />
</Head>
<BrowserFrame
html={`
<label class="label">
<!-- The real radio -->
<input type="radio" class="label__input" />
<!-- The fake circle -->
<div class="label__circle">
<!-- The inner circle -->
<div class="label__radio label__radio--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__circle {
/* Rounded border */
border: 1px solid #d1d5db;
border-radius: 9999px;
/* Spacing */
margin-right: 8px;
padding: 4px;
}
.label__radio {
/* Rounded border */
border-radius: 9999px;
height: 16px;
width: 16px;
/* For not selected radio */
background-color: transparent;
}
.label__radio--selected {
/* For selected radio */
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',
}}
>
<Radio value="1">
<div style={{ width: '100px' }}>
<Rectangle />
</div>
</Radio>
</div>
<div
style={{
alignItems: 'center',
display: 'inline-flex',
marginBottom: '16px',
}}
>
<Radio value="2">
<div style={{ width: '200px' }}>
<Rectangle />
</div>
</Radio>
</div>
<div
style={{
alignItems: 'center',
display: 'inline-flex',
}}
>
<Radio value="3">
<div style={{ width: '150px' }}>
<Rectangle />
</div>
</Radio>
</div>
</div>
</div>
</BrowserFrame>
<Spacer size="extraLarge" />
<RelatedPatterns patterns={[Pattern.CustomCheckboxButton, Pattern.RadioButtonGroup]} />
</PatternLayout>
);
};
export default Details;

View File

@@ -1,75 +0,0 @@
import * as React from 'react';
import Circle from '../../placeholders/Circle';
import Frame from '../../placeholders/Frame';
import Rectangle from '../../placeholders/Rectangle';
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: '9999px',
display: 'flex',
justifyContent: 'center',
marginRight: '8px',
padding: '4px',
}}
>
<Circle backgroundColor="#00449E" />
</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: '9999px',
display: 'flex',
justifyContent: 'center',
marginRight: '8px',
padding: '4px',
}}
>
<Circle backgroundColor="transparent" />
</div>
<div style={{ flex: 1 }}>
<Rectangle />
</div>
</div>
</div>
</Frame>
);
};
export default Cover;

View File

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

View File

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