Skip to content

Commit 440884c

Browse files
committed
feat(toggle): add 'size' attribute
Note: this removes 3 toggle related variables from pui-variables.scss [Finishes #134017213]
1 parent 4d79c94 commit 440884c

File tree

5 files changed

+105
-58
lines changed

5 files changed

+105
-58
lines changed

library/spec/pivotal-ui-react/toggle/toggle_spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,35 @@ describe('Toggle', () => {
7070
expect('.toggle-switch').toBeChecked();
7171
});
7272
});
73+
74+
describe('size attribute', function() {
75+
beforeEach(function() {
76+
ReactDOM.unmountComponentAtNode(root);
77+
});
78+
79+
it('renders with size=medium by default', function() {
80+
ReactDOM.render(<Toggle id='boggle' />, root);
81+
expect($('label').attr('class')).toContain('medium');
82+
});
83+
84+
it('uses size=small class on label', function() {
85+
ReactDOM.render(<Toggle id='boggle' size='small' />, root);
86+
expect($('label').attr('class')).toContain('small');
87+
});
88+
89+
it('uses size=medium class on label', function() {
90+
ReactDOM.render(<Toggle id='boggle' size='medium' />, root);
91+
expect($('label').attr('class')).toContain('medium');
92+
});
93+
94+
it('uses size=large class on label', function() {
95+
ReactDOM.render(<Toggle id='boggle' size='large' />, root);
96+
expect($('label').attr('class')).toContain('large');
97+
});
98+
99+
it('defaults to medium when size=some-random-string', function() {
100+
ReactDOM.render(<Toggle id='boggle' size='some-random-string' />, root);
101+
expect($('label').attr('class')).toContain('medium');
102+
});
103+
});
73104
});

library/src/pivotal-ui-react/toggle/toggle.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ const types = React.PropTypes;
77
class Toggle extends React.Component {
88
static propTypes = {
99
id: types.string,
10-
onChange: types.func
10+
onChange: types.func,
11+
size: types.string
1112
};
1213

1314
render() {
14-
const {onChange, children, id, ...others} = this.props;
15+
const {onChange, children, id, size, ...others} = this.props;
1516
const toggleId = id || uniqueid('toggle');
17+
const toggleSize = size && this.validSize(size) ? size : 'medium';
1618

17-
const props = mergeProps(others,
19+
const inputProps = mergeProps(others,
1820
{
1921
className: 'toggle-switch',
2022
id: toggleId,
@@ -25,11 +27,15 @@ class Toggle extends React.Component {
2527

2628
return (
2729
<div className='form-group'>
28-
<input {...props}/>
29-
<label htmlFor={toggleId}>{children}</label>
30+
<input {...inputProps}/>
31+
<label htmlFor={toggleId} className={toggleSize}>{children}</label>
3032
</div>
3133
)
3234
}
35+
36+
validSize(size) {
37+
return ['small', 'medium', 'large'].includes(size)
38+
}
3339
}
3440

3541
module.exports = {Toggle};

library/src/pivotal-ui/components/forms/forms.scss

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,48 @@ This form looks and behaves mostly like a content editable field
285285
cursor: pointer;
286286
outline: none;
287287
text-indent: -99999px;
288-
width: $toggle-bg-width;
289-
height: $toggle-bg-height;
290-
border-radius: $toggle-bg-height;
288+
289+
&.small {
290+
height: 14px;
291+
width: 26px;
292+
border-radius: 14px;
293+
294+
&:after {
295+
height: 12px;
296+
width: 12px;
297+
298+
top: 1px;
299+
left: 1px;
300+
}
301+
}
302+
303+
&.medium {
304+
height: 18px;
305+
width: 33px;
306+
border-radius: 18px;
307+
308+
&:after {
309+
height: 14px;
310+
width: 14px;
311+
312+
top: 2px;
313+
left: 2px;
314+
}
315+
}
316+
317+
&.large {
318+
height: 24px;
319+
width: 43px;
320+
border-radius: 24px;
321+
322+
&:after {
323+
height: 18px;
324+
width: 18px;
325+
326+
top: 3px;
327+
left: 3px;
328+
}
329+
}
291330

292331
&:after {
293332
position: absolute;
@@ -298,63 +337,27 @@ This form looks and behaves mostly like a content editable field
298337
background-color: $neutral-11;
299338
transition: margin 0.4s, background 0.4s;
300339
content: "";
301-
width: $toggle-diameter;
302-
height: $toggle-diameter;
303-
border-radius: $toggle-diameter / 2;
340+
border-radius: 50%;
304341
}
305342
}
306343

307344
&:checked + label {
308345
background-color: $blue-2;
309346
}
310347

311-
&[disabled] + label {
312-
@include opacity(.5);
348+
&:checked + label.small:after {
349+
margin-left: 12px;
313350
}
314351

315-
&:checked + label:after {
316-
margin-left: $toggle-bg-width - $toggle-diameter - 2;
352+
&:checked + label.medium:after {
353+
margin-left: 15px;
317354
}
318355

319-
&.toggle-switch-lg {
320-
+ label {
321-
width: 80px;
322-
height: 40px;
323-
border-radius: 40px;
324-
325-
&:after {
326-
top: 5px;
327-
left: 5px;
328-
width: 30px;
329-
height: 30px;
330-
border-radius: 15px;
331-
}
332-
}
356+
&:checked + label.large:after {
357+
margin-left: 19px;
358+
}
333359

334-
&:checked + label:after {
335-
margin-left: 40px;
336-
}
360+
&[disabled] + label {
361+
@include opacity(.5);
337362
}
338363
}
339-
340-
/*pending
341-
---
342-
title: Focus Inputs
343-
name: form_focus_input
344-
categories:
345-
- Forms
346-
- All
347-
---
348-
349-
Add the focus-input directive to an element that should be focused if the focus-input expression changes such that it evalutes as true.
350-
351-
```html_example
352-
<a ng-click="focus_input1=true; focus_input2=false;">Focus input 1</a>
353-
<br/>
354-
<a ng-click="focus_input2=true; focus_input1=false;">Focus input 2</a>
355-
<br/>
356-
<input focus-input="focus_input1"></input>
357-
<input focus-input="focus_input2"></input>
358-
```
359-
*/
360-

library/src/pivotal-ui/components/pui-variables.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,6 @@ $input-group-addon-bg: $gray-lighter !default;
504504
$input-group-addon-border-color: $input-border !default;
505505
$cursor-disabled: not-allowed !default;
506506

507-
$toggle-bg-width: 22px;
508-
$toggle-bg-height: 12px;
509-
$toggle-diameter: 10px;
510-
511507
// Dropdowns
512508
// -------------------------
513509

styleguide/docs/react/toggle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,15 @@ Note that you must handle the addition and removal of the `checked` property you
2929
<Toggle checked onChange={() => console.log('I should handle check changes!')}/>
3030
```
3131
32+
Toggle has a `size` attribute that takes three options; small, medium (default), and large.
33+
34+
```react_example
35+
<div>
36+
<Toggle size='small'/>
37+
<Toggle size='medium'/>
38+
<Toggle/>
39+
<Toggle size='large'/>
40+
</div>
41+
```
42+
3243
*/

0 commit comments

Comments
 (0)