Skip to content

Commit

Permalink
feat: Add new environment prop to handle tabs in iframes (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntadpear authored Feb 6, 2021
1 parent 4949d07 commit d6fd355
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Provide a custom class name for disabled tabs.
Register a callback that will receive the underlying DOM node for every mount. It will also receive null on unmount.
#### environment: `Window`
If you're rendering `react-tabs` within a different `window` context than the default one; for example, an iframe.
#### forceRenderTabPanel: `boolean`
> default: `false`
Expand Down
2 changes: 2 additions & 0 deletions src/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Tabs extends Component {
forceRenderTabPanel: false,
selectedIndex: null,
defaultIndex: null,
environment: null,
};

static propTypes = {
Expand All @@ -36,6 +37,7 @@ export default class Tabs extends Component {
selectedIndex: selectedIndexPropType,
selectedTabClassName: PropTypes.string,
selectedTabPanelClassName: PropTypes.string,
environment: PropTypes.object,
};

constructor(props) {
Expand Down
44 changes: 31 additions & 13 deletions src/components/UncontrolledTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ function isTabDisabled(node) {
}

let canUseActiveElement;
try {
canUseActiveElement = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.activeElement
);
} catch (e) {
// Work around for IE bug when accessing document.activeElement in an iframe
// Refer to the following resources:
// http://stackoverflow.com/a/10982960/369687
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12733599
canUseActiveElement = false;

function determineCanUseActiveElement(environment) {
const env =
environment || (typeof window !== 'undefined' ? window : undefined);

try {
canUseActiveElement = !!(
typeof env !== 'undefined' &&
env.document &&
env.document.activeElement
);
} catch (e) {
// Work around for IE bug when accessing document.activeElement in an iframe
// Refer to the following resources:
// http://stackoverflow.com/a/10982960/369687
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12733599
canUseActiveElement = false;
}
}
export default class UncontrolledTabs extends Component {
static defaultProps = {
Expand All @@ -57,6 +63,7 @@ export default class UncontrolledTabs extends Component {
selectedIndex: PropTypes.number.isRequired,
selectedTabClassName: PropTypes.string,
selectedTabPanelClassName: PropTypes.string,
environment: PropTypes.object,
};

tabNodes = [];
Expand Down Expand Up @@ -164,6 +171,7 @@ export default class UncontrolledTabs extends Component {
selectedIndex,
selectedTabClassName,
selectedTabPanelClassName,
environment,
} = this.props;

this.tabIds = this.tabIds || [];
Expand All @@ -190,10 +198,19 @@ export default class UncontrolledTabs extends Component {
// If it is we should keep the focus on the next selected tab
let wasTabFocused = false;

if (canUseActiveElement == null) {
determineCanUseActiveElement(environment);
}

if (canUseActiveElement) {
wasTabFocused = React.Children.toArray(child.props.children)
.filter(isTab)
.some((tab, i) => document.activeElement === this.getTab(i));
.some((tab, i) => {
const env =
environment ||
(typeof window !== 'undefined' ? window : undefined);
return env && env.document.activeElement === this.getTab(i);
});
}

result = cloneElement(child, {
Expand Down Expand Up @@ -350,6 +367,7 @@ export default class UncontrolledTabs extends Component {
selectedIndex, // unused
selectedTabClassName, // unused
selectedTabPanelClassName, // unused
environment, // unused
...attributes
} = this.props;

Expand Down

0 comments on commit d6fd355

Please sign in to comment.