Improved Responsiveness controllers - "remember" latest state of selected breakpoint.
This commit is contained in:
1 parent
7b7a8c101f
commit
ae68a88fd1
3 files changed
+22
-36
No files matched your search
Vendored
+10
-17
@@ -11180,16 +11180,12 @@ function ResponsiveButton({
|
|||||||
active,
|
active,
|
||||||
onSelect
|
onSelect
|
||||||
}) {
|
}) {
|
||||||
// active = true;
|
|
||||||
const [state, setState] = react.exports.useState({
|
const [state, setState] = react.exports.useState({
|
||||||
open: true,
|
open: true,
|
||||||
breakpoint: defaultResponsiveOptions[mode]
|
activeBreakpoint: defaultResponsiveOptions[mode]
|
||||||
});
|
});
|
||||||
|
|
||||||
const updateState = update => {
|
const updateState = update => setState(Object.assign({}, state, update));
|
||||||
// update.open = true;
|
|
||||||
setState(Object.assign({}, state, update));
|
|
||||||
};
|
|
||||||
|
|
||||||
react.exports.useEffect(() => {
|
react.exports.useEffect(() => {
|
||||||
const closeDropdown = e => isEscHit(e) ? updateState({
|
const closeDropdown = e => isEscHit(e) ? updateState({
|
||||||
@@ -11206,11 +11202,10 @@ function ResponsiveButton({
|
|||||||
};
|
};
|
||||||
}); // Blur event / Outside click
|
}); // Blur event / Outside click
|
||||||
|
|
||||||
const outsideClickAction = async e => (await isClickOutside(e)) ? updateState({
|
const handleBlur = async e => (await isClickOutside(e)) ? updateState({
|
||||||
open: false
|
open: false
|
||||||
}) : null;
|
}) : null;
|
||||||
|
|
||||||
const handleBlur = react.exports.useCallback(outsideClickAction, []);
|
|
||||||
return /*#__PURE__*/React.createElement(ResponsiveButtonStyle, {
|
return /*#__PURE__*/React.createElement(ResponsiveButtonStyle, {
|
||||||
tabIndex: "0",
|
tabIndex: "0",
|
||||||
onBlur: handleBlur
|
onBlur: handleBlur
|
||||||
@@ -11220,27 +11215,27 @@ function ResponsiveButton({
|
|||||||
onClick: () => select()
|
onClick: () => select()
|
||||||
}, mode), active && state.open && responsiveOptions[mode] && /*#__PURE__*/React.createElement(ResponsiveOptionsDropdown, null, responsiveOptions[mode].map(breakpoint => {
|
}, mode), active && state.open && responsiveOptions[mode] && /*#__PURE__*/React.createElement(ResponsiveOptionsDropdown, null, responsiveOptions[mode].map(breakpoint => {
|
||||||
return /*#__PURE__*/React.createElement("li", null, /*#__PURE__*/React.createElement("a", {
|
return /*#__PURE__*/React.createElement("li", null, /*#__PURE__*/React.createElement("a", {
|
||||||
className: state.breakpoint === breakpoint ? 'active' : '',
|
className: state.activeBreakpoint === breakpoint ? 'active' : '',
|
||||||
onClick: () => select(breakpoint)
|
onClick: () => select(breakpoint)
|
||||||
}, breakpoint));
|
}, breakpoint));
|
||||||
}))); //
|
}))); //
|
||||||
// Actions
|
// Actions
|
||||||
//
|
//
|
||||||
|
|
||||||
function select(breakpoint = null) {
|
function select(activeBreakpoint = null) {
|
||||||
// Click on option in Dropdown.
|
// Click on option in Dropdown.
|
||||||
if (breakpoint) {
|
if (activeBreakpoint) {
|
||||||
updateState({
|
updateState({
|
||||||
open: false,
|
open: false,
|
||||||
breakpoint
|
activeBreakpoint
|
||||||
});
|
});
|
||||||
onSelect(breakpoint);
|
onSelect(activeBreakpoint);
|
||||||
return;
|
return;
|
||||||
} // Click on device button.
|
} // Click on device button.
|
||||||
|
|
||||||
|
|
||||||
if (!active) {
|
if (!active) {
|
||||||
onSelect(state.breakpoint);
|
onSelect(state.activeBreakpoint);
|
||||||
updateState({
|
updateState({
|
||||||
open: false
|
open: false
|
||||||
});
|
});
|
||||||
@@ -11254,9 +11249,7 @@ function ResponsiveButton({
|
|||||||
async function isClickOutside(e) {
|
async function isClickOutside(e) {
|
||||||
const currentTarget = e.currentTarget;
|
const currentTarget = e.currentTarget;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
setTimeout(() => {
|
setTimeout(() => resolve(!currentTarget.contains(document.activeElement)));
|
||||||
resolve(!currentTarget.contains(document.activeElement));
|
|
||||||
}, 100);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function isEscHit(event) {
|
function isEscHit(event) {
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
{"version":3,"file":"index.min.js","sources":["../../../node_modules/react/cjs/react.production.min.js","../../../node_modules/react/index.js","../../../node_modules/scheduler/cjs/scheduler.production.min.js","../../../node_modules/scheduler/index.js","../../../node_modules/react-dom/cjs/react-dom.production.min.js","../../../node_modules/react-dom/index.js","../../../node_modules/react-dom/client.js","../../../node_modules/react-is/cjs/react-is.production.min.js","../../../node_modules/react-is/index.js","../../../node_modules/@emotion/stylis/dist/stylis.esm.js","../../../node_modules/@emotion/unitless/dist/unitless.esm.js","../../../node_modules/@emotion/memoize/dist/emotion-memoize.esm.js","../../../node_modules/@emotion/is-prop-valid/dist/emotion-is-prop-valid.esm.js","../../../node_modules/hoist-non-react-statics/node_modules/react-is/cjs/react-is.production.min.js","../../../node_modules/hoist-non-react-statics/node_modules/react-is/index.js","../../../node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.cjs.js","../toolbar/responsive.style.js","../toolbar/responsive-button/responsive-button.style.js","../toolbar/responsive-button/ResponsiveButton.jsx","../toolbar/responsive.jsx","../index.js"],"sourcesContent":["/**\n * @license React\n * react.production.min.js\n *\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n'use strict';var l=Symbol.for(\"react.element\"),n=Symbol.for(\"react.portal\"),p=Symbol.for(\"react.fragment\"),q=Symbol.for(\"react.strict_mode\"),r=Symbol.for(\"react.profiler\"),t=Symbol.for(\"react.provider\"),u=Symbol.for(\"react.context\"),v=Symbol.for(\"react.forward_ref\"),w=Symbol.for(\"react.suspense\"),x=Symbol.for(\"react.memo\"),y=Symbol.for(\"react.lazy\"),z=Symbol.iterator;function A(a){if(null===a||\"object\"!==typeof a)return null;a=z&&a[z]||a[\"@@iterator\"];return\"function\"===typeof a?a:null}\nvar B={isMounted:function(){return!1},enqueueForceUpdate:function(){},enqueueReplaceState:function(){},enqueueSetState:function(){}},C=Object.assign,D={};function E(a,b,e){this.props=a;this.context=b;this.refs=D;this.updater=e||B}E.prototype.isReactComponent={};\nE.prototype.setState=function(a,b){if(\"object\"!==typeof a&&\"function\"!==typeof a&&null!=a)throw Error(\"setState(...): takes an object of state variables to update or a function which returns an object of state variables.\");this.updater.enqueueSetState(this,a,b,\"setState\")};E.prototype.forceUpdate=function(a){this.updater.enqueueForceUpdate(this,a,\"forceUpdate\")};function F(){}F.prototype=E.prototype;function G(a,b,e){this.props=a;this.context=b;this.refs=D;this.updater=e||B}var H=G.prototype=new F;\nH.constructor=G;C(H,E.prototype);H.isPureReactComponent=!0;var I=Array.isArray,J=Object.prototype.hasOwnProperty,K={current:null},L={key:!0,ref:!0,__self:!0,__source:!0};\nfunction M(a,b,e){var d,c={},k=null,h=null;if(null!=b)for(d in void 0!==b.ref&&(h=b.ref),void 0!==b.key&&(k=\"\"+b.key),b)J.call(b,d)&&!L.hasOwnProperty(d)&&(c[d]=b[d]);var g=arguments.length-2;if(1===g)c.children=e;else if(1<g){for(var f=Array(g),m=0;m<g;m++)f[m]=arguments[m+2];c.children=f}if(a&&a.defaultProps)for(d in g=a.defaultProps,g)void 0===c[d]&&(c[d]=g[d]);return{$$typeof:l,type:a,key:k,ref:h,props:c,_owner:K.current}}\nfunction N(a,b){return{$$typeof:l,type:a.type,key:b,ref:a.ref,props:a.props,_owner:a._owner}}function O(a){return\"object\"===typeof a&&null!==a&&a.$$typeof===l}function escape(a){var b={\"=\":\"=0\",\":\":\"=2\"};return\"$\"+a.replace(/[=:]/g,function(a){return b[a]})}var P=/\\/+/g;function Q(a,b){return\"object\"===typeof a&&null!==a&&null!=a.key?escape(\"\"+a.key):b.toString(36)}\nfunction R(a,b,e,d,c){var k=typeof a;if(\"undefined\"===k||\"boolean\"===k)a=null;var h=!1;if(null===a)h=!0;else switch(k){case \"string\":case \"number\":h=!0;break;case \"object\":switch(a.$$typeof){case l:case n:h=!0}}if(h)return h=a,c=c(h),a=\"\"===d?\".\"+Q(h,0):d,I(c)?(e=\"\",null!=a&&(e=a.replace(P,\"$&/\")+\"/\"),R(c,b,e,\"\",function(a){return a})):null!=c&&(O(c)&&(c=N(c,e+(!c.key||h&&h.key===c.key?\"\":(\"\"+c.key).replace(P,\"$&/\")+\"/\")+a)),b.push(c)),1;h=0;d=\"\"===d?\".\":d+\":\";if(I(a))for(var g=0;g<a.length;g++){k=\na[g];var f=d+Q(k,g);h+=R(k,b,e,f,c)}else if(f=A(a),\"function\"===typeof f)for(a=f.call(a),g=0;!(k=a.next()).done;)k=k.value,f=d+Q(k,g++),h+=R(k,b,e,f,c);else if(\"object\"===k)throw b=String(a),Error(\"Objects are not valid as a React child (found: \"+(\"[object Object]\"===b?\"object with keys {\"+Object.keys(a).join(\", \")+\"}\":b)+\"). If you meant to render a collection of children, use an array instead.\");return h}\nfunction S(a,b,e){if(null==a)return a;var d=[],c=0;R(a,d,\"\",\"\",function(a){return b.call(e,a,c++)});return d}function T(a){if(-1===a._status){var b=a._result;b=b();b.then(function(b){if(0===a._status||-1===a._status)a._status=1,a._resuLine truncated
|
{"version":3,"file":"index.min.js","sources":["../../../node_modules/react/cjs/react.production.min.js","../../../node_modules/react/index.js","../../../node_modules/scheduler/cjs/scheduler.production.min.js","../../../node_modules/scheduler/index.js","../../../node_modules/react-dom/cjs/react-dom.production.min.js","../../../node_modules/react-dom/index.js","../../../node_modules/react-dom/client.js","../../../node_modules/react-is/cjs/react-is.production.min.js","../../../node_modules/react-is/index.js","../../../node_modules/@emotion/stylis/dist/stylis.esm.js","../../../node_modules/@emotion/unitless/dist/unitless.esm.js","../../../node_modules/@emotion/memoize/dist/emotion-memoize.esm.js","../../../node_modules/@emotion/is-prop-valid/dist/emotion-is-prop-valid.esm.js","../../../node_modules/hoist-non-react-statics/node_modules/react-is/cjs/react-is.production.min.js","../../../node_modules/hoist-non-react-statics/node_modules/react-is/index.js","../../../node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.cjs.js","../toolbar/responsive.style.js","../toolbar/responsive-button/responsive-button.style.js","../toolbar/responsive-button/ResponsiveButton.jsx","../toolbar/responsive.jsx","../index.js"],"sourcesContent":["/**\n * @license React\n * react.production.min.js\n *\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n'use strict';var l=Symbol.for(\"react.element\"),n=Symbol.for(\"react.portal\"),p=Symbol.for(\"react.fragment\"),q=Symbol.for(\"react.strict_mode\"),r=Symbol.for(\"react.profiler\"),t=Symbol.for(\"react.provider\"),u=Symbol.for(\"react.context\"),v=Symbol.for(\"react.forward_ref\"),w=Symbol.for(\"react.suspense\"),x=Symbol.for(\"react.memo\"),y=Symbol.for(\"react.lazy\"),z=Symbol.iterator;function A(a){if(null===a||\"object\"!==typeof a)return null;a=z&&a[z]||a[\"@@iterator\"];return\"function\"===typeof a?a:null}\nvar B={isMounted:function(){return!1},enqueueForceUpdate:function(){},enqueueReplaceState:function(){},enqueueSetState:function(){}},C=Object.assign,D={};function E(a,b,e){this.props=a;this.context=b;this.refs=D;this.updater=e||B}E.prototype.isReactComponent={};\nE.prototype.setState=function(a,b){if(\"object\"!==typeof a&&\"function\"!==typeof a&&null!=a)throw Error(\"setState(...): takes an object of state variables to update or a function which returns an object of state variables.\");this.updater.enqueueSetState(this,a,b,\"setState\")};E.prototype.forceUpdate=function(a){this.updater.enqueueForceUpdate(this,a,\"forceUpdate\")};function F(){}F.prototype=E.prototype;function G(a,b,e){this.props=a;this.context=b;this.refs=D;this.updater=e||B}var H=G.prototype=new F;\nH.constructor=G;C(H,E.prototype);H.isPureReactComponent=!0;var I=Array.isArray,J=Object.prototype.hasOwnProperty,K={current:null},L={key:!0,ref:!0,__self:!0,__source:!0};\nfunction M(a,b,e){var d,c={},k=null,h=null;if(null!=b)for(d in void 0!==b.ref&&(h=b.ref),void 0!==b.key&&(k=\"\"+b.key),b)J.call(b,d)&&!L.hasOwnProperty(d)&&(c[d]=b[d]);var g=arguments.length-2;if(1===g)c.children=e;else if(1<g){for(var f=Array(g),m=0;m<g;m++)f[m]=arguments[m+2];c.children=f}if(a&&a.defaultProps)for(d in g=a.defaultProps,g)void 0===c[d]&&(c[d]=g[d]);return{$$typeof:l,type:a,key:k,ref:h,props:c,_owner:K.current}}\nfunction N(a,b){return{$$typeof:l,type:a.type,key:b,ref:a.ref,props:a.props,_owner:a._owner}}function O(a){return\"object\"===typeof a&&null!==a&&a.$$typeof===l}function escape(a){var b={\"=\":\"=0\",\":\":\"=2\"};return\"$\"+a.replace(/[=:]/g,function(a){return b[a]})}var P=/\\/+/g;function Q(a,b){return\"object\"===typeof a&&null!==a&&null!=a.key?escape(\"\"+a.key):b.toString(36)}\nfunction R(a,b,e,d,c){var k=typeof a;if(\"undefined\"===k||\"boolean\"===k)a=null;var h=!1;if(null===a)h=!0;else switch(k){case \"string\":case \"number\":h=!0;break;case \"object\":switch(a.$$typeof){case l:case n:h=!0}}if(h)return h=a,c=c(h),a=\"\"===d?\".\"+Q(h,0):d,I(c)?(e=\"\",null!=a&&(e=a.replace(P,\"$&/\")+\"/\"),R(c,b,e,\"\",function(a){return a})):null!=c&&(O(c)&&(c=N(c,e+(!c.key||h&&h.key===c.key?\"\":(\"\"+c.key).replace(P,\"$&/\")+\"/\")+a)),b.push(c)),1;h=0;d=\"\"===d?\".\":d+\":\";if(I(a))for(var g=0;g<a.length;g++){k=\na[g];var f=d+Q(k,g);h+=R(k,b,e,f,c)}else if(f=A(a),\"function\"===typeof f)for(a=f.call(a),g=0;!(k=a.next()).done;)k=k.value,f=d+Q(k,g++),h+=R(k,b,e,f,c);else if(\"object\"===k)throw b=String(a),Error(\"Objects are not valid as a React child (found: \"+(\"[object Object]\"===b?\"object with keys {\"+Object.keys(a).join(\", \")+\"}\":b)+\"). If you meant to render a collection of children, use an array instead.\");return h}\nfunction S(a,b,e){if(null==a)return a;var d=[],c=0;R(a,d,\"\",\"\",function(a){return b.call(e,a,c++)});return d}function T(a){if(-1===a._status){var b=a._result;b=b();b.then(function(b){if(0===a._status||-1===a._status)a._status=1,a._resuLine truncated
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, {useCallback, useEffect, useState} from "react";
|
import React, {useEffect, useState} from "react";
|
||||||
import {ButtonStyling, ResponsiveButtonStyle, ResponsiveOptionsDropdown} from "./responsive-button.style.js";
|
import {ButtonStyling, ResponsiveButtonStyle, ResponsiveOptionsDropdown} from "./responsive-button.style.js";
|
||||||
|
|
||||||
const responsiveOptions = {
|
const responsiveOptions = {
|
||||||
@@ -15,12 +15,8 @@ const defaultResponsiveOptions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ResponsiveButton({mode, active, onSelect}) {
|
export function ResponsiveButton({mode, active, onSelect}) {
|
||||||
// active = true;
|
const [state, setState] = useState({open: true, activeBreakpoint: defaultResponsiveOptions[mode]});
|
||||||
const [state, setState] = useState({open: true, breakpoint: defaultResponsiveOptions[mode]});
|
const updateState = (update) => setState(Object.assign({}, state, update));
|
||||||
const updateState = (update) => {
|
|
||||||
// update.open = true;
|
|
||||||
setState(Object.assign({}, state, update));
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const closeDropdown = (e) => isEscHit(e) ? updateState({open: false}) : null;
|
const closeDropdown = (e) => isEscHit(e) ? updateState({open: false}) : null;
|
||||||
@@ -36,8 +32,7 @@ export function ResponsiveButton({mode, active, onSelect}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Blur event / Outside click
|
// Blur event / Outside click
|
||||||
const outsideClickAction = async (e) => await isClickOutside(e) ? updateState({open: false}) : null;
|
const handleBlur = async (e) => await isClickOutside(e) ? updateState({open: false}) : null;
|
||||||
const handleBlur = useCallback(outsideClickAction, []);
|
|
||||||
|
|
||||||
return <ResponsiveButtonStyle tabIndex='0' onBlur={handleBlur}>
|
return <ResponsiveButtonStyle tabIndex='0' onBlur={handleBlur}>
|
||||||
<ButtonStyling data-mode={mode} data-active={active} onClick={() => select()}>{mode}</ButtonStyling>
|
<ButtonStyling data-mode={mode} data-active={active} onClick={() => select()}>{mode}</ButtonStyling>
|
||||||
@@ -45,7 +40,7 @@ export function ResponsiveButton({mode, active, onSelect}) {
|
|||||||
<ResponsiveOptionsDropdown>
|
<ResponsiveOptionsDropdown>
|
||||||
{responsiveOptions[mode].map((breakpoint) => {
|
{responsiveOptions[mode].map((breakpoint) => {
|
||||||
return <li>
|
return <li>
|
||||||
<a className={state.breakpoint === breakpoint ? 'active' : ''} onClick={() => select(breakpoint)}>{breakpoint}</a>
|
<a className={state.activeBreakpoint === breakpoint ? 'active' : ''} onClick={() => select(breakpoint)}>{breakpoint}</a>
|
||||||
</li>;
|
</li>;
|
||||||
})}
|
})}
|
||||||
</ResponsiveOptionsDropdown>
|
</ResponsiveOptionsDropdown>
|
||||||
@@ -56,17 +51,17 @@ export function ResponsiveButton({mode, active, onSelect}) {
|
|||||||
// Actions
|
// Actions
|
||||||
//
|
//
|
||||||
|
|
||||||
function select(breakpoint = null) {
|
function select(activeBreakpoint = null) {
|
||||||
// Click on option in Dropdown.
|
// Click on option in Dropdown.
|
||||||
if (breakpoint) {
|
if (activeBreakpoint) {
|
||||||
updateState({open: false, breakpoint});
|
updateState({open: false, activeBreakpoint});
|
||||||
onSelect(breakpoint);
|
onSelect(activeBreakpoint);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Click on device button.
|
// Click on device button.
|
||||||
if (!active) {
|
if (!active) {
|
||||||
onSelect(state.breakpoint)
|
onSelect(state.activeBreakpoint)
|
||||||
updateState({open: false});
|
updateState({open: false});
|
||||||
} else {
|
} else {
|
||||||
updateState({open: true});
|
updateState({open: true});
|
||||||
@@ -82,9 +77,7 @@ export async function isClickOutside(e) {
|
|||||||
const currentTarget = e.currentTarget;
|
const currentTarget = e.currentTarget;
|
||||||
|
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
setTimeout(() => {
|
setTimeout(() => resolve(!currentTarget.contains(document.activeElement)));
|
||||||
resolve(!currentTarget.contains(document.activeElement));
|
|
||||||
}, 100);
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user