Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for switch to take custom colors #426

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/Switch/Switch-styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { StyledFormControlLabel } from '../Form/Form-styled';
// Icons

// Third party libraries
import { transparentize } from 'polished';
import { transparentize, lighten, darken } from 'polished';

const switchProps = {
switchWidth: '36px',
Expand Down Expand Up @@ -257,6 +257,49 @@ const StyledSwitchTrack = styled.span`
}
`};

${props =>
props.color &&
css`
input:hover + &:after {
border-color: ${lighten(0.2, props.color)};
}

input:active + &:after {
border-color: ${lighten(0.2, props.color)};
}

input:checked:active + & {
box-shadow: 0 0 4px 2px
${transparentize(0.6, lighten(0.2, props.color))};

&:after {
border-color: ${lighten(0.2, props.color)};
}
}

input:checked + & {
background-color: ${props.color};
border-color: ${darken(0.2, props.color)};

&:after {
border-color: ${darken(0.2, props.color)};
}
}

input:focus + &:after {
border-color: ${lighten(0.2, props.color)};
}

input:checked:focus + & {
box-shadow: 0 0 4px 2px
${transparentize(0.6, lighten(0.2, props.color))};

&:after {
border-color: ${lighten(0.2, props.color)};
}
}
`};

// alignment fixes for edge
@supports (-ms-ime-align: auto) {
& {
Expand Down
5 changes: 4 additions & 1 deletion src/Switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Switch = ({
labelPosition,
fullWidth,
destructive,
color,
checked,
field,
form,
Expand Down Expand Up @@ -98,7 +99,7 @@ const Switch = ({
{...other}
type="checkbox"
/>
<StyledSwitchTrack destructive={destructive} />
<StyledSwitchTrack destructive={destructive} color={color} />
{labelPosition === 'after' ? getSwitchLabel(children) : null}
</StyledSwitch>
);
Expand All @@ -115,6 +116,8 @@ Switch.propTypes = {
onChange: PropTypes.func,
/** Should use a red highlight color. */
destructive: PropTypes.bool,
/** The color of the Switch. 'green' | 'rgb(0, 255, 255)' | '#fefefe' */
color: PropTypes.string,
/** Position of the label text in relation to the input. */
labelPosition: PropTypes.oneOf(['before', 'after']),
/** Switch and label will take up the full width of the container */
Expand Down
31 changes: 29 additions & 2 deletions src/Switch/doc/Switch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A skin on a checkbox.
#### Import Syntax

```js
import Switch from 'calcite-react/Switch'
import Switch from 'calcite-react/Switch';
```

## Basic Usage
Expand All @@ -40,14 +40,41 @@ import Switch from 'calcite-react/Switch'
</GuideExample>
</Playground>

## Alternate Colors

<Playground>
<GuideExample label="color">
<Switch checked color="green">
green
</Switch>
<Switch checked color="#64DAA1">
#64DAA1
</Switch>
<Switch checked color="#060080">
#060080
</Switch>
<Switch checked color="rgb(127,255,0)">
rgb(127,255,0)
</Switch>
<Switch checked color="rgb(32,178,170)">
rgb(32,178,170)
</Switch>
<Switch checked color="rgb(165,42,42)">
rgb(165,42,42)
</Switch>
</GuideExample>
</Playground>

## fullWidth

<Playground>
<GuideExample>
<Switch fullWidth>Enable Two-Factor Authentication</Switch>
</GuideExample>
<GuideExample>
<Switch fullWidth labelPosition="before">Enable Two-Factor Authentication</Switch>
<Switch fullWidth labelPosition="before">
Enable Two-Factor Authentication
</Switch>
</GuideExample>
</Playground>

Expand Down