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

Commit 9f66a14

Browse files
authored
fix(FocusZone): fix last breaking changes and make improvements when using Chat (#614)
* fix(FocusZone): fix last breaking changes and make improvements when using Chat * set explicit query selector on boolean attributes * fix propTypes * set explicit query selector on boolean attributes * Small fix of console error * update changelog * update changelog
1 parent faeef51 commit 9f66a14

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2323
### Fixes
2424
- Ensure `Popup` properly flips values of `offset` prop in RTL @kuzhelov ([#612](https://github.com/stardust-ui/react/pull/612))
2525
- Fix `List` - items should be selectable @sophieH29 ([#566](https://github.com/stardust-ui/react/pull/566))
26-
- Respect `defaultTabbable` element when `FocusZone` container gets focus ([#637](https://github.com/stardust-ui/react/pull/637))
26+
- Respect `defaultTabbable` element when `FocusZone` container gets focus @sophieH29 ([#637](https://github.com/stardust-ui/react/pull/637))
27+
- Fix `FocusZone` - fix last breaking changes and make improvements for `Chat` usage @sophieH29 ([#614](https://github.com/stardust-ui/react/pull/614))
2728

2829
### Features
2930
- Add `color` prop to `Text` component @Bugaa92 ([#597](https://github.com/stardust-ui/react/pull/597))

docs/src/prototypes/chatPane/services/messageFactoryMock.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function createMessageContentWithAttachments(content: string, messageId: string)
119119
icon="file word outline"
120120
aria-label={`File attachment ${fileName}. Press tab for more options Press Enter to open the file`}
121121
header={fileName}
122-
action={render => render(actionPopup)}
122+
action={actionPopup}
123123
data-is-focusable={true}
124124
styles={{
125125
'&:focus': {

src/lib/accessibility/Behaviors/Chat/chatBehavior.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ChatBehavior: Accessibility = (props: any) => ({
2121
props: {
2222
shouldEnterInnerZone: event => keyboardKey.getCode(event) === keyboardKey.Enter,
2323
direction: FocusZoneDirection.vertical,
24-
shouldHandleKeyDownCapture: false,
24+
shouldResetActiveElementWhenTabFromZone: true,
2525
defaultTabbableElement: getLastTabbableElement, // select last chat message by default
2626
[CHAT_FOCUSZONE_ATTRIBUTE]: '', // allows querying the default active element
2727
},
@@ -36,8 +36,11 @@ const ChatBehavior: Accessibility = (props: any) => ({
3636
})
3737

3838
const getLastTabbableElement = (root: HTMLElement): HTMLElement => {
39+
const lastVisibleMessage = root.querySelector('[data-last-visible="true"]') as HTMLElement
40+
if (lastVisibleMessage) return lastVisibleMessage
41+
3942
const chatItemsElements = root.querySelectorAll(
40-
`[${CHAT_FOCUSZONE_ATTRIBUTE}] .ui-chat__item > [${IS_FOCUSABLE_ATTRIBUTE}]`,
43+
`[${CHAT_FOCUSZONE_ATTRIBUTE}] .ui-chat__message[${IS_FOCUSABLE_ATTRIBUTE}="true"]`,
4144
)
4245
return chatItemsElements.length > 0
4346
? (chatItemsElements[chatItemsElements.length - 1] as HTMLElement)

src/lib/accessibility/FocusZone/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is a list of changes made to this Stardust copy of FocusZone in comparison
88
- Fix `defaultTabbableElement` prop to be as a function ([#450](https://github.com/stardust-ui/react/pull/450))
99
- Remove role="presentation" @sophieH29 ([#530](https://github.com/stardust-ui/react/pull/530))
1010
- Respect `defaultTabbable` element when FocusZone container receives focus @sophieH29 ([#637](https://github.com/stardust-ui/react/pull/637))
11+
- Fix `FocusZone` - add `shouldResetActiveElementWhenTabFromZone` prop @sophieH29 ([#614](https://github.com/stardust-ui/react/pull/614))
1112

1213
### Features
1314
- Add embed mode for FocusZone and new Chat behavior ([#233](https://github.com/stardust-ui/react/pull/233))

src/lib/accessibility/FocusZone/FocusZone.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
4747
direction: PropTypes.number,
4848
defaultTabbableElement: PropTypes.func,
4949
shouldFocusOnMount: PropTypes.bool,
50+
shouldResetActiveElementWhenTabFromZone: PropTypes.bool,
5051
shouldFocusInnerElementWhenReceivedFocus: PropTypes.bool,
51-
shouldHandleKeyDownCapture: PropTypes.bool,
5252
disabled: PropTypes.bool,
5353
as: customPropTypes.as,
5454
isCircularNavigation: PropTypes.bool,
@@ -67,7 +67,6 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
6767
static defaultProps: FocusZoneProps = {
6868
isCircularNavigation: false,
6969
direction: FocusZoneDirection.bidirectional,
70-
shouldHandleKeyDownCapture: true,
7170
as: 'div',
7271
}
7372

@@ -105,8 +104,6 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
105104
public componentDidMount(): void {
106105
_allInstances[this._id] = this
107106

108-
const { shouldHandleKeyDownCapture, defaultTabbableElement, shouldFocusOnMount } = this.props
109-
110107
this.setRef(this) // called here to support functional components, we only need HTMLElement ref anyway
111108
if (this._root.current) {
112109
this.windowElement = getWindow(this._root.current)
@@ -121,27 +118,22 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
121118
parentElement = getParent(parentElement)
122119
}
123120

124-
if (!this._isInnerZone && shouldHandleKeyDownCapture) {
121+
if (!this._isInnerZone) {
125122
this.windowElement.addEventListener('keydown', this.onKeyDownCapture, true)
126123
}
127124

128125
// Assign initial tab indexes so that we can set initial focus as appropriate.
129126
this.updateTabIndexes()
130127

131-
if (defaultTabbableElement) {
132-
const initialActiveElement = defaultTabbableElement(this._root.current)
133-
initialActiveElement && this.setActiveElement(initialActiveElement)
134-
}
135-
136-
if (shouldFocusOnMount) {
128+
if (this.props.shouldFocusOnMount) {
137129
this.focus()
138130
}
139131
}
140132
}
141133

142134
public componentWillUnmount() {
143135
delete _allInstances[this._id]
144-
if (this.windowElement && this.props.shouldHandleKeyDownCapture) {
136+
if (this.windowElement) {
145137
this.windowElement.removeEventListener('keydown', this.onKeyDownCapture, true)
146138
}
147139
}
@@ -490,6 +482,8 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
490482
if (focusChanged) {
491483
break
492484
}
485+
} else if (this.props.shouldResetActiveElementWhenTabFromZone) {
486+
this._activeElement = null
493487
}
494488
return undefined
495489

@@ -885,6 +879,11 @@ export class FocusZone extends React.Component<FocusZoneProps> implements IFocus
885879

886880
private updateTabIndexes(onElement?: HTMLElement) {
887881
let element = onElement
882+
883+
if (!this._activeElement && this.props.defaultTabbableElement) {
884+
this._activeElement = this.props.defaultTabbableElement(this._root.current)
885+
}
886+
888887
if (!element && this._root.current) {
889888
this._defaultFocusElement = null
890889
element = this._root.current

src/lib/accessibility/FocusZone/FocusZone.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export interface FocusZoneProps extends React.HTMLAttributes<HTMLElement | Focus
6060
shouldFocusInnerElementWhenReceivedFocus?: boolean
6161

6262
/**
63-
* If global onKeyDownCapture should be handled and updating tab indexes.
63+
* If true and handleTab is false, resets current activeElement to null value.
6464
*/
65-
shouldHandleKeyDownCapture?: boolean
65+
shouldResetActiveElementWhenTabFromZone?: boolean
6666

6767
/**
6868
* If set, the FocusZone will not be tabbable and keyboard navigation will be disabled.

0 commit comments

Comments
 (0)