@10up/block-components
Version:
10up Components built for the WordPress Block Editor.
372 lines (333 loc) • 12.5 kB
JavaScript
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./stores/icons/actions.ts":
/*!*********************************!*\
!*** ./stores/icons/actions.ts ***!
\*********************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ registerIconSet: function() { return /* binding */ registerIconSet; },
/* harmony export */ removeIconSet: function() { return /* binding */ removeIconSet; }
/* harmony export */ });
/**
* Returns an action object used in signalling that new block styles have been added.
*
* @param {object} iconSet icon set.
*
* @returns {object} Action object.
*/
function registerIconSet(iconSet) {
return {
type: 'REGISTER_ICON_SET',
iconSet
};
}
/**
* Returns an action object used in signalling that block styles have been removed.
*
* @param {string} name Icon Set name.
*
* @returns {object} Action object.
*/
function removeIconSet(name) {
return {
type: 'REMOVE_ICON_SET',
name
};
}
/***/ }),
/***/ "./stores/icons/index.ts":
/*!*******************************!*\
!*** ./stores/icons/index.ts ***!
\*******************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ store: function() { return /* binding */ store; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _reducer__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./reducer */ "./stores/icons/reducer.ts");
/* harmony import */ var _selectors__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./selectors */ "./stores/icons/selectors.ts");
/* harmony import */ var _actions__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./actions */ "./stores/icons/actions.ts");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const STORE_NAME = 'tenup/icons';
/**
* Store definition for the icons namespace.
*
* @type {object}
*/
const store = (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.createReduxStore)(STORE_NAME, {
reducer: _reducer__WEBPACK_IMPORTED_MODULE_1__["default"],
selectors: _selectors__WEBPACK_IMPORTED_MODULE_2__,
actions: _actions__WEBPACK_IMPORTED_MODULE_3__
});
const hasStore = !!(0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.select)(STORE_NAME);
if (!hasStore) {
(0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.register)(store);
}
/***/ }),
/***/ "./stores/icons/reducer.ts":
/*!*********************************!*\
!*** ./stores/icons/reducer.ts ***!
\*********************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": function() { return /* binding */ reducer; }
/* harmony export */ });
/* eslint-disable default-param-last */
/*
* Reducer managing the block style variations.
*/
function reducer(state = {
iconSets: {}
}, action) {
switch (action.type) {
case 'REGISTER_ICON_SET':
return {
...state,
iconSets: {
...state.iconSets,
[action.iconSet.name]: action.iconSet
}
};
case 'REMOVE_ICON_SET':
// eslint-disable-next-line no-prototype-builtins
if (state.iconSets.hasOwnProperty(action.name)) {
const newState = {
...state
};
delete newState.iconSets[action.name];
return newState;
}
return state;
default:
return state;
}
}
/***/ }),
/***/ "./stores/icons/selectors.ts":
/*!***********************************!*\
!*** ./stores/icons/selectors.ts ***!
\***********************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ getIcon: function() { return /* binding */ getIcon; },
/* harmony export */ getIconSet: function() { return /* binding */ getIconSet; },
/* harmony export */ getIconSets: function() { return /* binding */ getIconSets; },
/* harmony export */ getIcons: function() { return /* binding */ getIcons; }
/* harmony export */ });
/* eslint-disable no-prototype-builtins */
/**
* Returns all icons sets
*
* @param {object} state Data state.
*
* @returns {Array?} Icon Sets.
*/
function getIconSets(state) {
const {
iconSets
} = state;
return Object.values(iconSets);
}
/**
* Returns an Icon Set by its name
*
* @param {object} state Data state.
* @param {string} name Name of the Icon Set.
*
* @returns {object?} Icon Set.
*/
function getIconSet(state, name) {
const {
iconSets
} = state;
const iconSet = iconSets[name] ?? [];
return iconSet;
}
/**
* Returns an icon of an icon set by its name
*
* @param {object} state Data state.
* @param {string} name Name of the Icon Set.
*
* @returns {Array?} List of Icons.
*/
function getIcons(state, name) {
const {
iconSets
} = state;
return iconSets?.hasOwnProperty(name) ? iconSets[name]?.icons ?? [] : [];
}
/**
* Returns an icon of an icon set by its name
*
* @param {object} state Data state.
* @param {string} name Name of the Icon Set.
* @param {string} iconName Name of the iconName.
*
* @returns {object?} Icon.
*/
function getIcon(state, name, iconName) {
const {
iconSets
} = state;
return iconSets?.hasOwnProperty(name) ? iconSets[name]?.icons?.find(item => item.name === iconName) ?? [] : undefined;
}
/***/ }),
/***/ "./stores/index.ts":
/*!*************************!*\
!*** ./stores/index.ts ***!
\*************************/
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ iconStore: function() { return /* reexport safe */ _icons__WEBPACK_IMPORTED_MODULE_0__.store; }
/* harmony export */ });
/* harmony import */ var _icons__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./icons */ "./stores/icons/index.ts");
/***/ }),
/***/ "@wordpress/data":
/*!**********************************!*\
!*** external "@wordpress/data" ***!
\**********************************/
/***/ (function(module) {
module.exports = require("@wordpress/data");
/***/ }),
/***/ "@wordpress/element":
/*!*************************************!*\
!*** external "@wordpress/element" ***!
\*************************************/
/***/ (function(module) {
module.exports = require("@wordpress/element");
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ !function() {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function() { return module['default']; } :
/******/ function() { return module; };
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = function(exports, definition) {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ !function() {
/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
/******/ }();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ !function() {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ }();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
!function() {
/*!**********************************!*\
!*** ./hooks/use-icons/index.ts ***!
\**********************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ useIcon: function() { return /* binding */ useIcon; },
/* harmony export */ useIcons: function() { return /* binding */ useIcons; }
/* harmony export */ });
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @wordpress/data */ "@wordpress/data");
/* harmony import */ var _wordpress_data__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_wordpress_data__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/element */ "@wordpress/element");
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _stores__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../stores */ "./stores/index.ts");
function transformIcons(iconSet) {
return iconSet.icons.map(icon => ({
...icon,
iconSet: iconSet.name
}));
}
const useIcons = (iconSet = '') => {
const [icons, setIcons] = (0,_wordpress_element__WEBPACK_IMPORTED_MODULE_1__.useState)([]);
const rawIcons = (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
const {
getIconSet,
getIconSets
} = select(_stores__WEBPACK_IMPORTED_MODULE_2__.iconStore);
if (iconSet) {
return getIconSet(iconSet);
}
return getIconSets();
}, [iconSet]);
(0,_wordpress_element__WEBPACK_IMPORTED_MODULE_1__.useEffect)(() => {
if (iconSet) {
setIcons(transformIcons(rawIcons));
} else {
setIcons(Object.values(rawIcons).reduce((rawIcons, iconSet) => [...rawIcons, ...transformIcons(iconSet)], []));
}
}, [rawIcons, iconSet]);
return icons;
};
const useIcon = (iconSet, name) => {
return (0,_wordpress_data__WEBPACK_IMPORTED_MODULE_0__.useSelect)(select => {
return select(_stores__WEBPACK_IMPORTED_MODULE_2__.iconStore).getIcon(iconSet, name);
}, [iconSet, name]);
};
}();
module.exports = __webpack_exports__;
/******/ })()
;
//# sourceMappingURL=index.js.map