@khanacademy/wonder-blocks-clickable 2.1.2 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/es/index.js +27 -25
- package/dist/index.js +593 -286
- package/package.json +9 -10
- package/src/components/__tests__/clickable-behavior.test.js +47 -2
- package/src/components/__tests__/clickable.test.js +19 -12
- package/src/components/clickable-behavior.js +9 -9
- package/src/components/clickable.js +16 -13
- package/src/components/clickable.md +7 -0
- package/src/components/clickable.stories.js +8 -10
- package/src/util/get-clickable-behavior.js +2 -2
- package/LICENSE +0 -21
package/CHANGELOG.md
ADDED
package/dist/es/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose';
|
|
2
2
|
import _extends from '@babel/runtime/helpers/extends';
|
|
3
|
-
import
|
|
3
|
+
import * as React from 'react';
|
|
4
4
|
import { StyleSheet } from 'aphrodite';
|
|
5
|
-
import { any } from 'prop-types';
|
|
6
5
|
import { withRouter, Link } from 'react-router-dom';
|
|
6
|
+
import { __RouterContext } from 'react-router';
|
|
7
7
|
import { addStyle } from '@khanacademy/wonder-blocks-core';
|
|
8
8
|
import Color from '@khanacademy/wonder-blocks-color';
|
|
9
9
|
|
|
@@ -52,7 +52,9 @@ const disabledHandlers = {
|
|
|
52
52
|
onKeyUp: () => void 0,
|
|
53
53
|
onFocus: () => void 0,
|
|
54
54
|
onBlur: () => void 0,
|
|
55
|
-
|
|
55
|
+
// Clickable components should still be tabbable so they can
|
|
56
|
+
// be used as anchors.
|
|
57
|
+
tabIndex: 0
|
|
56
58
|
};
|
|
57
59
|
const keyCodes = {
|
|
58
60
|
enter: 13,
|
|
@@ -138,12 +140,12 @@ const startState = {
|
|
|
138
140
|
* The react-router aware version is returned if `router` is a react-router-dom
|
|
139
141
|
* router, `skipClientNav` is not `true`, and `href` is an internal URL.
|
|
140
142
|
*
|
|
141
|
-
* The `router` can be accessed via
|
|
142
|
-
* rendered as a descendant of a BrowserRouter.
|
|
143
|
+
* The `router` can be accessed via __RouterContext (imported from 'react-router')
|
|
144
|
+
* from a component rendered as a descendant of a BrowserRouter.
|
|
143
145
|
* See https://reacttraining.com/react-router/web/guides/basic-components.
|
|
144
146
|
*/
|
|
145
147
|
|
|
146
|
-
class ClickableBehavior extends Component {
|
|
148
|
+
class ClickableBehavior extends React.Component {
|
|
147
149
|
static getDerivedStateFromProps(props, state) {
|
|
148
150
|
// If new props are disabled, reset the hovered/focused/pressed states
|
|
149
151
|
if (props.disabled) {
|
|
@@ -516,8 +518,8 @@ const isClientSideUrl = href => {
|
|
|
516
518
|
* The react-router aware version is returned if `router` is a react-router-dom
|
|
517
519
|
* router, `skipClientNav` is not `true`, and `href` is an internal URL.
|
|
518
520
|
*
|
|
519
|
-
* The `router` can be accessed via
|
|
520
|
-
* as a descendant of a BrowserRouter.
|
|
521
|
+
* The `router` can be accessed via __RouterContext (imported from 'react-router')
|
|
522
|
+
* from a component rendered as a descendant of a BrowserRouter.
|
|
521
523
|
* See https://reacttraining.com/react-router/web/guides/basic-components.
|
|
522
524
|
*/
|
|
523
525
|
const ClickableBehaviorWithRouter = withRouter(ClickableBehavior);
|
|
@@ -574,31 +576,31 @@ const StyledLink = addStyle(Link);
|
|
|
574
576
|
* ```
|
|
575
577
|
*/
|
|
576
578
|
|
|
577
|
-
class Clickable extends Component {
|
|
579
|
+
class Clickable extends React.Component {
|
|
578
580
|
constructor(...args) {
|
|
579
581
|
super(...args);
|
|
580
582
|
|
|
581
|
-
this.getCorrectTag = (clickableState, commonProps) => {
|
|
583
|
+
this.getCorrectTag = (clickableState, router, commonProps) => {
|
|
582
584
|
const activeHref = this.props.href && !this.props.disabled;
|
|
583
|
-
const useClient =
|
|
585
|
+
const useClient = router && !this.props.skipClientNav && isClientSideUrl(this.props.href || ""); // NOTE: checking this.props.href here is redundant, but flow
|
|
584
586
|
// needs it to refine this.props.href to a string.
|
|
585
587
|
|
|
586
588
|
if (activeHref && useClient && this.props.href) {
|
|
587
|
-
return /*#__PURE__*/createElement(StyledLink, _extends({}, commonProps, {
|
|
589
|
+
return /*#__PURE__*/React.createElement(StyledLink, _extends({}, commonProps, {
|
|
588
590
|
to: this.props.href,
|
|
589
591
|
role: this.props.role,
|
|
590
592
|
target: this.props.target || undefined,
|
|
591
593
|
"aria-disabled": this.props.disabled ? "true" : undefined
|
|
592
594
|
}), this.props.children(clickableState));
|
|
593
595
|
} else if (activeHref && !useClient) {
|
|
594
|
-
return /*#__PURE__*/createElement(StyledAnchor, _extends({}, commonProps, {
|
|
596
|
+
return /*#__PURE__*/React.createElement(StyledAnchor, _extends({}, commonProps, {
|
|
595
597
|
href: this.props.href,
|
|
596
598
|
role: this.props.role,
|
|
597
599
|
target: this.props.target || undefined,
|
|
598
600
|
"aria-disabled": this.props.disabled ? "true" : undefined
|
|
599
601
|
}), this.props.children(clickableState));
|
|
600
602
|
} else {
|
|
601
|
-
return /*#__PURE__*/createElement(StyledButton, _extends({}, commonProps, {
|
|
603
|
+
return /*#__PURE__*/React.createElement(StyledButton, _extends({}, commonProps, {
|
|
602
604
|
type: "button",
|
|
603
605
|
disabled: this.props.disabled
|
|
604
606
|
}), this.props.children(clickableState));
|
|
@@ -606,7 +608,7 @@ class Clickable extends Component {
|
|
|
606
608
|
};
|
|
607
609
|
}
|
|
608
610
|
|
|
609
|
-
|
|
611
|
+
renderClickableBehavior(router) {
|
|
610
612
|
const _this$props = this.props,
|
|
611
613
|
{
|
|
612
614
|
href,
|
|
@@ -625,12 +627,12 @@ class Clickable extends Component {
|
|
|
625
627
|
} = _this$props,
|
|
626
628
|
restProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
|
|
627
629
|
|
|
628
|
-
const ClickableBehavior = getClickableBehavior(href, skipClientNav,
|
|
630
|
+
const ClickableBehavior = getClickableBehavior(href, skipClientNav, router);
|
|
629
631
|
|
|
630
632
|
const getStyle = state => [styles.reset, styles.link, !hideDefaultFocusRing && state.focused && (light ? styles.focusedLight : styles.focused), style];
|
|
631
633
|
|
|
632
634
|
if (beforeNav) {
|
|
633
|
-
return /*#__PURE__*/createElement(ClickableBehavior, {
|
|
635
|
+
return /*#__PURE__*/React.createElement(ClickableBehavior, {
|
|
634
636
|
href: href,
|
|
635
637
|
onClick: onClick,
|
|
636
638
|
beforeNav: beforeNav,
|
|
@@ -638,12 +640,12 @@ class Clickable extends Component {
|
|
|
638
640
|
onKeyDown: onKeyDown,
|
|
639
641
|
onKeyUp: onKeyUp,
|
|
640
642
|
disabled: disabled
|
|
641
|
-
}, (state, childrenProps) => this.getCorrectTag(state, _extends({}, restProps, {
|
|
643
|
+
}, (state, childrenProps) => this.getCorrectTag(state, router, _extends({}, restProps, {
|
|
642
644
|
"data-test-id": testId,
|
|
643
645
|
style: getStyle(state)
|
|
644
646
|
}, childrenProps)));
|
|
645
647
|
} else {
|
|
646
|
-
return /*#__PURE__*/createElement(ClickableBehavior, {
|
|
648
|
+
return /*#__PURE__*/React.createElement(ClickableBehavior, {
|
|
647
649
|
href: href,
|
|
648
650
|
onClick: onClick,
|
|
649
651
|
safeWithNav: safeWithNav,
|
|
@@ -651,18 +653,19 @@ class Clickable extends Component {
|
|
|
651
653
|
onKeyUp: onKeyUp,
|
|
652
654
|
target: target,
|
|
653
655
|
disabled: disabled
|
|
654
|
-
}, (state, childrenProps) => this.getCorrectTag(state, _extends({}, restProps, {
|
|
656
|
+
}, (state, childrenProps) => this.getCorrectTag(state, router, _extends({}, restProps, {
|
|
655
657
|
"data-test-id": testId,
|
|
656
658
|
style: getStyle(state)
|
|
657
659
|
}, childrenProps)));
|
|
658
660
|
}
|
|
659
661
|
}
|
|
660
662
|
|
|
663
|
+
render() {
|
|
664
|
+
return /*#__PURE__*/React.createElement(__RouterContext.Consumer, null, router => this.renderClickableBehavior(router));
|
|
665
|
+
}
|
|
666
|
+
|
|
661
667
|
} // Source: https://gist.github.com/MoOx/9137295
|
|
662
668
|
|
|
663
|
-
Clickable.contextTypes = {
|
|
664
|
-
router: any
|
|
665
|
-
};
|
|
666
669
|
Clickable.defaultProps = {
|
|
667
670
|
light: false,
|
|
668
671
|
disabled: false,
|
|
@@ -708,5 +711,4 @@ const styles = StyleSheet.create({
|
|
|
708
711
|
}
|
|
709
712
|
});
|
|
710
713
|
|
|
711
|
-
export default
|
|
712
|
-
export { ClickableBehavior, getClickableBehavior, isClientSideUrl };
|
|
714
|
+
export { ClickableBehavior, Clickable as default, getClickableBehavior, isClientSideUrl };
|