-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support high-contrast detection, fixes #424
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
layout: doc | ||
title: delite/hc | ||
--- | ||
|
||
# delite/hc | ||
|
||
Provides "High Contrast" feature detection for accessibility purposes. | ||
|
||
This is useful for Internet Explorer and Firefox running on Windows. | ||
It doesn't apply to Chrome or Safari (on mobile, mac, or desktop), which don't support high contrast mode. | ||
|
||
## Introduction | ||
|
||
By doing a `require()` of `delite/hc`, `delite/hc` will set the `has("highcontrast")` flag | ||
so code can check directly whether or not it is in high contrast mode and branch appropriately. | ||
If the machine is in high contrast mode, `has("highcontrast")` is the text color being used by the browser | ||
(that overrides the text color defined in CSS rules). | ||
|
||
|
||
`delite/hc` will also add the `d-hc` CSS class to your document's `<body>` tag if the machine is in high contrast mode. | ||
|
||
Normally this module should not be used. As long as widgets and applications avoid using background images for | ||
icons, the browser will do everything for high contrast mode automatically. The exception is for SVG, | ||
which the browser does not adjust. | ||
|
||
|
||
## Usage | ||
|
||
Simply require the module, and then reference `has("highcontrast")`: | ||
|
||
|
||
```js | ||
require (["delite/hc", ...), function (has, ...) { | ||
... | ||
var hcColor = has("highcontrast"); | ||
if (hcColor) { | ||
mySvgNode.style.color = hcColor; | ||
} | ||
}); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Test if computer is in high contrast mode (i.e. if CSS color settings are ignored) | ||
* Defines `has("highcontrast")` and sets `d-hc` CSS class on `<body>` if machine is in high contrast mode. | ||
* | ||
* Normally this code should not be used. As long as widgets or applications avoid using background images for | ||
* icons, the browser will do everything for high contrast mode automatically. The exception is for SVG, | ||
* which the browser does not adjust. | ||
* | ||
* If the OS is in high contrast mode and the browser obeys the OS setting, | ||
* `has("highcontrast")` is the color that text appears as. Otherwise, `has("highcontrast")` is null. | ||
* | ||
* Module returns `has()` method. | ||
* | ||
* @module delite/hc | ||
*/ | ||
define([ | ||
"requirejs-dplugins/has", | ||
"requirejs-domready/domReady!" | ||
], function (has) { | ||
|
||
has.add("highcontrast", function () { | ||
if (typeof window === "undefined") { | ||
return false; | ||
} | ||
|
||
// note: if multiple documents, doesn't matter which one we use | ||
var div = document.createElement("div"); | ||
try { | ||
div.style.cssText = | ||
"border: 1px solid; border-color:red green; position: absolute; height: 5px; top: -999px;"; | ||
document.body.appendChild(div); | ||
|
||
var cs = getComputedStyle(div); | ||
|
||
// If it's high contrast mode then return the color and background color the browser is using. | ||
// Otherwise just return null | ||
return cs.borderTopColor === cs.borderRightColor ? cs.color : null; | ||
} catch (e) { | ||
console.warn("hccss: exception detecting high-contrast mode, document is likely hidden: " + e.toString()); | ||
return null; | ||
} finally { | ||
document.body.removeChild(div); | ||
} | ||
}); | ||
|
||
if (has("highcontrast")) { | ||
document.body.className = (document.body.className + " d-hc").trim(); | ||
} | ||
|
||
return has; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
<title>high contrast detection test</title> | ||
<script type="text/javascript" src="boilerplate.js"></script> | ||
<script language="JavaScript" type="text/javascript"> | ||
require([ | ||
"delite/hc", | ||
"requirejs-domready/domReady!" | ||
], function (has) { | ||
if (has("highcontrast")) { | ||
result.innerHTML = "High contrast mode detected, text color = " + has("highcontrast"); | ||
} else { | ||
result.innerHTML = "Not high contrast mode (or browser ignores OS high contrast mode setting)"; | ||
} | ||
}) | ||
</script> | ||
</head> | ||
<body> | ||
<h1>High Contrast Detection Test</h1> | ||
<p> | ||
Result: <span id="result"></span>. | ||
</p> | ||
<p> | ||
On Windows, setting the OS to high-contrast mode will make IE and FF ignore the CSS settings for | ||
text color and size, and use the OS's settings instead (typically white on black or black on white). | ||
Note that on Chrome, the OS setting is ignored. | ||
</p> | ||
<p> | ||
Text with CSS settings for color: | ||
<span style="color: red">red</span> | ||
<span style="color: green">green</span> | ||
<span style="color: blue">blue</span> | ||
</p> | ||
</body> | ||
</html> |