carbon-fields
carbon-fields copied to clipboard
Switches for selectors
Version
- Carbon Fields: 2.2.0
- WordPress: 4.9
- PHP: 7
Again for pretty theme option pages, iOS type swtiches.

Thanks for the suggestion @breath-ee, we will consider it in the next redesign. Currently we are looking to blend with the base WP style.
I support the idea to keep things the WP way... but let's admit that WP's UX is old and outdated. A switch/toggle control is a must nowadays, IMO
For now, it's possible to create switches with CSS and the radio field:
Field definition:
Field::make( 'radio', 'my_switch', esc_attr__( 'Activate?', 'my-text-domain' ) )
->set_options( array(
'no' => esc_attr__( 'No', 'my-text-domain' ),
'yes' => esc_attr__( 'Yes', 'my-text-domain' ),
) )
->set_default_value( 'no' )
->set_classes( 'my-switch' ),
SASS:
.my-switch {
& ul {
list-style-type: none;
display: inline-flex;
}
& ul li {
display: inline;
}
& .cf-field__body {
display: flex;
margin-bottom: 36px;
overflow: hidden;
}
& .cf-field__body input {
position: absolute !important;
clip: rect(0, 0, 0, 0);
height: 1px;
width: 1px;
border: 0;
overflow: hidden;
}
& .cf-field__body label {
background-color: #e4e4e4;
color: rgba(0, 0, 0, 0.6);
font-size: 14px;
line-height: 2.5;
text-align: center;
padding: 6px 16px;
margin-right: -1px;
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
transition: all 0.1s ease-in-out;
-webkit-tap-highlight-color: transparent;
}
& .cf-field__body label:hover {
cursor: pointer;
}
& .cf-field__body input:checked+label {
background-color: #2372b1;
color: #fff;
box-shadow: none;
}
& .cf-field__body li:first-of-type label {
border-radius: 4px 0 0 4px;
}
& .cf-field__body li:last-of-type label {
border-radius: 0 4px 4px 0;
}
}
Result:
