Skip to content

Commit

Permalink
Improve types and add tagName support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb committed Sep 8, 2018
1 parent 1e2b5d4 commit de6fe0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ To use, wrap your tree with this component.

## Props

### `toggle`

By default, outlines are permanently shown after being enabled.

Set the `toggle` prop to have React Outline Manager hide outlines again when the user resumes interacting with their
mouse or with touch.

### `className`

Pass in a `className` as a string.
|Prop |Default value |Description|
|-----------|---------------------|-----------|
|`className`|`ReactOutlineHandler`|Use this to change the class of the wrapping component. This class is only visible when outlines should be hidden.|
|`tagName` |`div` |Use this to change the element type used in the wrapping component.|
|`toggle` |`false` |By default, outlines are enabled when a user hits tab, but not hidden again. Set this option to `true` if you want outlines to toggle on and off as the user switches between input methods.|
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-outline-manager",
"version": "1.0.1",
"version": "1.1.0",
"description": "A simple helper for toggling CSS outlines.",
"keywords": [
"a11y",
Expand Down
29 changes: 17 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import * as React from 'react';

interface Props {
children: JSX.Element,
children?: JSX.Element,
className: string,
tagName: string,
toggle: boolean,
}

const DEFAULT_CLASSNAME = 'ReactOutlineHandler';

export default class ReactOutlineHander extends React.Component<Props, {}> {

state = {
isUsingKeyboard: false,
static defaultProps: Partial<Props> = {
className: 'ReactOutlineHandler',
tagName: 'div',
}

addListeners = () => {
window.addEventListener('keydown', this.handleTab);
if (this.props.toggle) window.addEventListener('mousedown', this.handleMouseDown);
state = {
isUsingKeyboard: false,
}

componentDidMount () {
Expand All @@ -28,6 +27,11 @@ export default class ReactOutlineHander extends React.Component<Props, {}> {
this.removeListeners();
}

addListeners = () => {
window.addEventListener('keydown', this.handleTab);
if (this.props.toggle) window.addEventListener('mousedown', this.handleMouseDown);
}

handleTab = ({ keyCode }: KeyboardEvent) => {
if (keyCode === 9) {
this.setState({ isUsingKeyboard: true });
Expand All @@ -41,7 +45,7 @@ export default class ReactOutlineHander extends React.Component<Props, {}> {

insertStyleTag = () => {
const script = document.createElement('style');
const className = this.props.className || DEFAULT_CLASSNAME;
const className = this.props.className;

script.id = className;
script.innerText = `.${className} a:focus,.${className} area:focus,.${className} button:focus,.${className} iframe:focus,.${className} input:focus,.${className} select:focus,.${className} textarea:focus,.${className} [tabindex]:focus,.${className} [contenteditable]:focus { outline: none; }`;
Expand All @@ -55,11 +59,12 @@ export default class ReactOutlineHander extends React.Component<Props, {}> {
}

render () {
const className = this.props.className || DEFAULT_CLASSNAME;
const className = this.props.className;
const Tag = this.props.tagName;
return (
<div {...!this.state.isUsingKeyboard ? { className } : null }>
<Tag {...!this.state.isUsingKeyboard ? { className } : null }>
{this.props.children}
</div>
</Tag>
);
}

Expand Down

0 comments on commit de6fe0d

Please sign in to comment.