Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

feat(Focus): focus indicator for additional documents #1816

Merged
merged 5 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Export `FocusTrapZoneProps` and `AutoFocusZoneProps` from the main package @sophieH29 ([#1795](https://github.com/stardust-ui/react/pull/1795))
- Add `checkable` and `checkableIndicator` to the `Dropdown` and `DropdownItem` components @mnajdova ([#1738](https://github.com/stardust-ui/react/pull/1738))
- Add unified ramp of category colors and category color schemes: `foreground`, `foreground1`, `background` to the Teams themes @natashamayurshah ([#1711](https://github.com/stardust-ui/react/pull/1711))
- Focus indicator for additional documents @jurokapsiar ([#1816](https://github.com/stardust-ui/react/pull/1816))

### Documentation
- Restore docs for `Ref` component @layershifter ([#1777](https://github.com/stardust-ui/react/pull/1777))
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/components/Provider/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from 'react'
// @ts-ignore
import { RendererProvider, ThemeProvider, ThemeContext } from '@stardust-ui/react-fela'

import { felaRenderer, ChildrenComponentProps } from '../../lib'
import { felaRenderer, ChildrenComponentProps, setUpWhatInput } from '../../lib'

import {
ThemePrepared,
Expand Down Expand Up @@ -136,6 +136,9 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {

componentDidMount() {
this.renderFontFaces()
if (this.props.target) {
setUpWhatInput(this.props.target)
}
}

render() {
Expand Down
43 changes: 30 additions & 13 deletions packages/react/src/lib/whatInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,45 @@ const setUp = () => {
// add correct mouse wheel event mapping to `inputMap`
inputMap[detectWheel()] = 'mouse'

addListeners()
addListeners(window)
jurokapsiar marked this conversation as resolved.
Show resolved Hide resolved
doUpdate()
}

/*
* events
*/

const addListeners = () => {
const addListeners = (eventTarget: Window) => {
// `pointermove`, `MSPointerMove`, `mousemove` and mouse wheel event binding
// can only demonstrate potential, but not actual, interaction
// and are treated separately
const options = supportsPassive ? { passive: true, useCapture: true } : true

// pointer events (mouse, pen, touch)
// @ts-ignore
if (window.PointerEvent) {
window.addEventListener('pointerdown', setInput)
if (eventTarget.PointerEvent) {
eventTarget.addEventListener('pointerdown', setInput)
// @ts-ignore
} else if (window.MSPointerEvent) {
window.addEventListener('MSPointerDown', setInput)
eventTarget.addEventListener('MSPointerDown', setInput)
} else {
// mouse events
window.addEventListener('mousedown', setInput, true)
eventTarget.addEventListener('mousedown', setInput, true)

// touch events
if ('ontouchstart' in window) {
window.addEventListener('touchstart', eventBuffer, options)
window.addEventListener('touchend', setInput, true)
if ('ontouchstart' in eventTarget) {
eventTarget.addEventListener('touchstart', eventBuffer, options)
eventTarget.addEventListener('touchend', setInput, true)
}
}

// keyboard events
window.addEventListener('keydown', eventBuffer, true)
window.addEventListener('keyup', eventBuffer, true)
eventTarget.addEventListener('keydown', eventBuffer, true)
eventTarget.addEventListener('keyup', eventBuffer, true)

// focus events
window.addEventListener('focusin', setElement)
window.addEventListener('focusout', clearElement)
eventTarget.addEventListener('focusin', setElement)
eventTarget.addEventListener('focusout', clearElement)
}

// checks conditions before updating new input
Expand Down Expand Up @@ -234,6 +234,23 @@ if (isBrowser() && 'addEventListener' in window && Array.prototype.indexOf) {
setUp()
}

/*
* set up for document
*/

export const setUpWhatInput = (target: Document) => {
if (isBrowser() && 'addEventListener' in window && Array.prototype.indexOf) {
jurokapsiar marked this conversation as resolved.
Show resolved Hide resolved
const whatInputInitialized = 'whatInputInitialized'
if (target[whatInputInitialized] === true) {
return
}
target[whatInputInitialized] = true

addListeners(target.defaultView)
doUpdate()
}
}

export const setWhatInputSource = (newInput: 'mouse' | 'keyboard' | 'initial') => {
currentInput = newInput
}
Expand Down