Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(switch): keyboard focus ink state
Browse files Browse the repository at this point in the history
No focus on click

Closes #1336
  • Loading branch information
Marcy Sutton committed Apr 10, 2015
1 parent ade76f9 commit 0e80219
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 36 deletions.
12 changes: 10 additions & 2 deletions src/components/checkbox/checkbox-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ md-checkbox.md-THEME_NAME-theme {
&.md-checked .md-ripple {
color: '{{background-600}}';
}
&.md-checked.focus .md-container:before {
background-color: '{{accent-color-0.26}}';
}

.md-icon {
border-color: '{{foreground-2}}';
Expand Down Expand Up @@ -34,6 +37,10 @@ md-checkbox.md-THEME_NAME-theme {
background-color: '{{primary-color-0.87}}';
}

&.md-checked.focus .md-container:before {
background-color: '{{primary-color-0.26}}';
}

&.md-checked .md-icon:after {
border-color: '{{background-200}}';
}
Expand All @@ -43,14 +50,15 @@ md-checkbox.md-THEME_NAME-theme {
.md-ripple {
color: '{{warn-600}}';
}

.md-icon {
border-color: '{{foreground-2}}';
}
&.md-checked .md-icon {
background-color: '{{warn-color-0.87}}';
}

&.md-checked.focus:not([disabled]) .md-container:before {
background-color: '{{warn-color-0.26}}';
}
&.md-checked .md-icon:after {
border-color: '{{background-200}}';
}
Expand Down
19 changes: 16 additions & 3 deletions src/components/checkbox/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ angular.module('material.components.checkbox', [
* </hljs>
*
*/
function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming, $mdUtil) {
function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
inputDirective = inputDirective[0];
var CHECKED_CSS = 'md-checked';

Expand Down Expand Up @@ -98,8 +98,21 @@ function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant,
0: {}
}, attr, [ngModelCtrl]);

element.on('click', listener)
.on('keypress', keypressHandler);
// restrict focus styles to the keyboard
scope.mouseActive = false;
element
.on('keypress', keypressHandler)
.on('mousedown', function() {
scope.mouseActive = true;
$timeout(function(){
scope.mouseActive = false;
}, 100);
})
.on('focus', function() {
if(scope.mouseActive === false) element.addClass('focus');
})
.on('blur', function() { element.removeClass('focus'); });

ngModelCtrl.$render = render;

function keypressHandler(ev) {
Expand Down
35 changes: 30 additions & 5 deletions src/components/checkbox/checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,43 @@ md-checkbox {
box-sizing: border-box;
}

&.focus:not([disabled]) {
.md-container:before {
left: -8px;
top: -8px;
right: -8px;
bottom: -8px;
}

&:not(.md-checked) {
.md-container:before {
background-color: rgba(0, 0, 0, 0.12);
}
}
}

.md-container {
position: relative;
top: 4px;
display: inline-block;
width: $checkbox-width;
height: $checkbox-height;

&:before {
background-color: transparent;
border-radius: 50%;
content: '';
position: absolute;
display: block;
height: auto;
left: 0;
top: 0;
right: 0;
bottom: 0;
transition: all 0.5s;
width: auto;
}

&:after {
content: '';
position: absolute;
Expand Down Expand Up @@ -65,11 +95,6 @@ md-checkbox {
cursor: no-drop;
}

// focus
&:focus .md-label:not(:empty) {
border-color: black;
}


&.md-checked .md-icon:after {
transform: rotate(45deg);
Expand Down
10 changes: 9 additions & 1 deletion src/components/radioButton/radio-button-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,13 @@ md-radio-button.md-THEME_NAME-theme {


md-radio-group.md-THEME_NAME-theme:focus:not(:empty) {
border-color: '{{foreground-1}}';
.md-checked .md-container:before {
background-color: '{{accent-color-0.26}}';
}
.md-checked:not([disabled]).md-primary .md-container:before {
background-color: '{{primary-color-0.26}}';
}
.md-checked.md-primary .md-container:before {
background-color: '{{warn-color-0.26}}';
}
}
27 changes: 22 additions & 5 deletions src/components/radioButton/radio-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ md-radio-button,
left: -$radio-width;
top: -$radio-width;
}

&:before {
background-color: transparent;
border-radius: 50%;
content: '';
position: absolute;
display: block;
height: auto;
left: 0;
top: 0;
right: 0;
bottom: 0;
transition: all 0.5s;
width: auto;
}
}


Expand Down Expand Up @@ -80,13 +95,15 @@ md-radio-button,
}

}
md-radio-group {
border: 1px dotted transparent;
display: block;
outline: none;
md-radio-group:focus .md-checked {
.md-container:before {
left: -8px;
top: -8px;
right: -8px;
bottom: -8px;
}
}


.radioButtondemoBasicUsage md-radio-group {
border: none;
}
Expand Down
9 changes: 8 additions & 1 deletion src/components/radioButton/radioButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming) {
$mdTheming(element);
var rgCtrl = ctrls[0];
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
scope.mouseActive = false;

function keydownListener(ev) {
switch(ev.keyCode) {
Expand Down Expand Up @@ -97,7 +98,13 @@ function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming) {
'role': 'radiogroup',
'tabIndex': element.attr('tabindex') || '0'
})
.on('keydown', keydownListener);
.on('keydown', keydownListener)
.on('mouseover', function() { scope.mouseActive = true; })
.on('mouseout', function() { scope.mouseActive = false; })
.on('focus', function() {
if(scope.mouseActive === false) element.addClass('focus');
})
.on('blur', function() { element.removeClass('focus'); });

}

Expand Down
9 changes: 4 additions & 5 deletions src/components/switch/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
Switch (Disabled, Active)
</md-switch>

<md-switch class="md-primary" md-no-ink aria-label="Switch No Ink">
<md-switch class="md-primary" md-no-ink aria-label="Switch No Ink" ng-model="data.cb5">
Switch (md-primary): No Ink
</md-switch>


<md-switch ng-model="data.cb5" aria-label="Switch 5" ng-change="onChange(data.cb5)">
Switch 5 message: {{ message }}

<md-switch ng-model="data.cb6" aria-label="Switch 6" ng-change="onChange(data.cb6)">
Switch 6 message: {{ message }}
</md-switch>
</div>
9 changes: 5 additions & 4 deletions src/components/switch/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ angular.module('switchDemo1', ['ngMaterial'])
.controller('SwitchDemoCtrl', function($scope) {
$scope.data = {
cb1: true,
cb4: true
cb4: true,
cb5: false
};
$scope.onChange = function(cbState){
$scope.message = "The switch is now: " + cbState;

$scope.onChange = function(cbState) {
$scope.message = "The switch is now: " + cbState;
};
});
17 changes: 9 additions & 8 deletions src/components/switch/switch-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ md-switch.md-THEME_NAME-theme {
.md-bar {
background-color: '{{accent-color-0.5}}';
}
&.focus .md-thumb:before {
background-color: '{{accent-color-0.26}}';
}

&.md-primary {
.md-thumb {
Expand All @@ -21,6 +24,9 @@ md-switch.md-THEME_NAME-theme {
.md-bar {
background-color: '{{primary-color-0.5}}';
}
&.focus .md-thumb:before {
background-color: '{{primary-color-0.26}}';
}
}

&.md-warn {
Expand All @@ -30,6 +36,9 @@ md-switch.md-THEME_NAME-theme {
.md-bar {
background-color: '{{warn-color-0.5}}';
}
&.focus .md-thumb:before {
background-color: '{{warn-color-0.26}}';
}
}
}

Expand All @@ -41,12 +50,4 @@ md-switch.md-THEME_NAME-theme {
background-color: '{{foreground-4}}';
}
}

&:focus {
.md-label:not(:empty) {
border-color: '{{foreground-1}}';
border-style: dotted;
}
}

}
11 changes: 11 additions & 0 deletions src/components/switch/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function MdSwitch(mdCheckboxDirective, $mdTheming, $mdUtil, $document, $mdConsta
var thumbContainer = angular.element(element[0].querySelector('.md-thumb-container'));
var switchContainer = angular.element(element[0].querySelector('.md-container'));

scope.mouseActive = false;

// no transition on initial load
$$rAF(function() {
element.removeClass('md-dragging');
Expand All @@ -93,6 +95,15 @@ function MdSwitch(mdCheckboxDirective, $mdTheming, $mdUtil, $document, $mdConsta
});
}

// keypress after clicking does not show focus circle
element
.on('mouseover', function() { scope.mouseActive = true; })
.on('mouseout', function() { scope.mouseActive = false; })
.on('focus', function() {
if(scope.mouseActive === false) element.addClass('focus');
})
.on('blur', function() { element.removeClass('focus'); });

// These events are triggered by setup drag
$mdGesture.register(switchContainer, 'drag');
switchContainer
Expand Down
30 changes: 30 additions & 0 deletions src/components/switch/switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ md-switch {
}
}

&.focus:not([disabled]) {
.md-thumb:before {
left: -8px;
top: -8px;
right: -8px;
bottom: -8px;
}

&:not(.md-checked) {
.md-thumb:before {
background-color: rgba(0, 0, 0, 0.12);
}
}
}

.md-label {
border-color: transparent;
border-width: 0px;
Expand Down Expand Up @@ -68,6 +83,21 @@ md-switch {
border-radius: 50%;
box-shadow: $whiteframe-shadow-z1;

&:before {
background-color: transparent;
border-radius: 50%;
content: '';
position: absolute;
display: block;
height: auto;
left: 0;
top: 0;
right: 0;
bottom: 0;
transition: all 0.5s;
width: auto;
}

.md-ripple-container {
position: absolute;
display: block;
Expand Down
Loading

0 comments on commit 0e80219

Please sign in to comment.