Improved Responsiveness controllers - "remember" latest state of selected breakpoint.
This commit is contained in:
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
File diff suppressed because one or more lines are too long
@@ -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