Skip to content

Add warning when Settings.setAppElement is not set. #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions components/app-launcher/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const defaultProps = {
assistiveText: {
trigger: 'Open App Launcher',
},
ariaHideApp: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React modal has this prop to "opt out" of setAppElement. I think we should use that as well.

title: 'App Launcher',
};

Expand Down Expand Up @@ -82,6 +83,10 @@ const AppLauncher = createReactClass({
assistiveText: PropTypes.shape({
trigger: PropTypes.string,
}),
/**
* Boolean indicating if the appElement should be hidden.
*/
ariaHideApp: PropTypes.bool,
/**
* One or more `<AppLauncherSection />`s each containing one or more `<AppLauncherTile />`s
*/
Expand Down Expand Up @@ -248,6 +253,7 @@ const AppLauncher = createReactClass({
</button>
</div>
<Modal
ariaHideApp={this.props.ariaHideApp}
contentClassName="slds-modal__content slds-app-launcher__content slds-p-around--medium"
contentStyle={{ minHeight: modalContentStaticHeight }}
isOpen={isOpen}
Expand Down
22 changes: 22 additions & 0 deletions components/component-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@
"computed": false
}
},
"ariaHideApp": {
"type": {
"name": "bool"
},
"required": false,
"description": "Boolean indicating if the appElement should be hidden. _Tested with snapshot testing._",
"defaultValue": {
"value": "true",
"computed": false
}
},
"children": {
"type": {
"name": "node"
Expand Down Expand Up @@ -7336,6 +7347,17 @@
"computed": false
}
},
"ariaHideApp": {
"type": {
"name": "bool"
},
"required": false,
"description": "Boolean indicating if the appElement should be hidden. _Tested with snapshot testing._",
"defaultValue": {
"value": "true",
"computed": false
}
},
"assistiveText": {
"type": {
"name": "shape",
Expand Down
11 changes: 11 additions & 0 deletions components/modal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import shortid from 'shortid';
// This component's `checkProps` which issues warnings to developers about properties when in development mode (similar to React's built in development tools)
import checkProps from './check-props';

import checkAppElementIsSet from '../../utilities/warning/check-app-element-set';

import Button from '../button';

import { MODAL } from '../../utilities/constants';
Expand All @@ -33,6 +35,10 @@ const propTypes = {
* Vertical alignment of Modal.
*/
align: PropTypes.oneOf(['top', 'center']),
/**
* Boolean indicating if the appElement should be hidden.
*/
ariaHideApp: PropTypes.bool,
/**
* **Assistive text for accessibility.**
* This object is merged with the default props object on every render.
Expand Down Expand Up @@ -150,6 +156,7 @@ const defaultProps = {
closeButton: 'Close',
},
align: 'center',
ariaHideApp: true,
dismissible: true,
};

Expand Down Expand Up @@ -181,6 +188,9 @@ class Modal extends React.Component {
componentWillMount () {
this.generatedId = shortid.generate();
checkProps(MODAL, this.props);
if (this.props.ariaHideApp) {
checkAppElementIsSet();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If consumer hasn't opted-out, check if the app element was set. The app-launcher component also uses this modal internally so I only need to add this check here.

}
}

componentDidMount () {
Expand Down Expand Up @@ -450,6 +460,7 @@ class Modal extends React.Component {

return (
<ReactModal
ariaHideApp={this.props.ariaHideApp}
contentLabel="Modal"
isOpen={this.props.isOpen}
onRequestClose={this.closeModal}
Expand Down
24 changes: 24 additions & 0 deletions utilities/warning/check-app-element-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
/* eslint-disable import/no-mutable-exports */

import warning from 'warning';
import Settings from '../../components/settings';

let checkAppElementIsSet = function () {};

if (process.env.NODE_ENV !== 'production') {
checkAppElementIsSet = function () {
const appElement = Settings.getAppElement();
/* eslint-disable max-len */
warning(
!!appElement,
'[Design System React] App element is not defined. Please use Settings.setAppElement(el).' +
' By default, `Modal` will add `aria-hidden=true` to the `body` tag, but this disables some assistive technologies.' +
' To prevent this you can add Settings.setAppElement(el) to your application with `el` being the root node of your application' +
' that you would like to hide from assistive technologies when the `Modal` is open.'
);
};
}

export default checkAppElementIsSet;