UNPKG

@braineet/ui

Version:

Braineet design system

119 lines (118 loc) 4.27 kB
"use strict"; exports.__esModule = true; exports.reducer = exports.defaultInitialState = exports.createStore = exports.actions = void 0; var _react = require("react"); function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var actions = { INIT: 'INIT', RESIZE_SCREEN: 'RESIZE_SCREEN', EXPAND_SIDE_NAVIGATION: 'EXPAND_SIDE_NAVIGATION', COLLAPSE_SIDE_NAVIGATION: 'COLLAPSE_SIDE_NAVIGATION', TOGGLE_SIDE_NAVIGATION: 'TOGGLE_SIDE_NAVIGATION', ON_CHANGE: 'ON_CHANGE' }; exports.actions = actions; var defaultInitialState = { initialized: false, defaultExpanded: undefined, expanded: undefined, width: 0, controlled: false }; exports.defaultInitialState = defaultInitialState; var reducer = function reducer(state, action) { var _action$size, _action$size2; switch (action.type) { case actions.INIT: { var _action$options, _action$options2; var initState = _extends({}, state, { initialized: true, width: (action == null ? void 0 : (_action$options = action.options) == null ? void 0 : _action$options.width) || 0 }); if (state.controlled) return initState; return _extends({}, initState, { expanded: (state == null ? void 0 : state.defaultExpanded) !== undefined ? state == null ? void 0 : state.defaultExpanded : (action == null ? void 0 : (_action$options2 = action.options) == null ? void 0 : _action$options2.width) > 992 }); } case actions.RESIZE_SCREEN: if (state.controlled) return state; return _extends({}, state, { width: (action == null ? void 0 : (_action$size = action.size) == null ? void 0 : _action$size.width) || 0, expanded: (action == null ? void 0 : (_action$size2 = action.size) == null ? void 0 : _action$size2.width) > 992 }); case actions.EXPAND_SIDE_NAVIGATION: if (state.controlled) return state; return _extends({}, state, { expanded: true }); case actions.COLLAPSE_SIDE_NAVIGATION: if (state.controlled) return state; return _extends({}, state, { expanded: false }); case actions.TOGGLE_SIDE_NAVIGATION: if (state.controlled) return state; return _extends({}, state, { expanded: state.expanded !== undefined ? !state.expanded : false }); case actions.ON_CHANGE: return _extends({}, state, { expanded: (action == null ? void 0 : action.expanded) || (state == null ? void 0 : state.expanded) }); default: return state; } }; exports.reducer = reducer; var createStore = function createStore(initialState) { // eslint-disable-next-line react-hooks/rules-of-hooks var _useReducer = (0, _react.useReducer)(reducer, _extends({}, defaultInitialState, initialState, { controlled: !!(initialState != null && initialState.expanded), expanded: (initialState == null ? void 0 : initialState.defaultExpanded) || (initialState == null ? void 0 : initialState.expanded) || undefined })), state = _useReducer[0], dispatch = _useReducer[1]; var onResize = function onResize(size) { return dispatch({ type: actions.RESIZE_SCREEN, size: size }); }; var init = function init(options) { return dispatch({ type: actions.INIT, options: options }); }; var expand = function expand() { return dispatch({ type: actions.EXPAND_SIDE_NAVIGATION }); }; var collapse = function collapse() { return dispatch({ type: actions.COLLAPSE_SIDE_NAVIGATION }); }; var toggle = function toggle() { return dispatch({ type: actions.TOGGLE_SIDE_NAVIGATION }); }; var onChange = function onChange(expanded) { return dispatch({ type: actions.ON_CHANGE, expanded: expanded }); }; return [state, { init: init, onResize: onResize, expand: expand, collapse: collapse, toggle: toggle, onChange: onChange }]; }; exports.createStore = createStore;