@automattic/jetpack-shared-extension-utils 0.11.5 → 0.12.1

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 CHANGED
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.12.1] - 2023-10-10
9
+ ### Changed
10
+ - Updated package dependencies. [#33428]
11
+
12
+ ## [0.12.0] - 2023-09-19
13
+ ### Added
14
+ - Helpers functions for block icons [#32698]
15
+
8
16
  ## [0.11.5] - 2023-09-13
9
17
  ### Changed
10
18
  - Updated package dependencies. [#33001]
@@ -252,6 +260,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
252
260
  ### Changed
253
261
  - Core: prepare utility for release
254
262
 
263
+ [0.12.1]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.12.0...0.12.1
264
+ [0.12.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.11.5...0.12.0
255
265
  [0.11.5]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.11.4...0.11.5
256
266
  [0.11.4]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.11.3...0.11.4
257
267
  [0.11.3]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/0.11.2...0.11.3
package/index.js CHANGED
@@ -17,3 +17,4 @@ export { default as isCurrentUserConnected } from './src/is-current-user-connect
17
17
  export { default as useAnalytics } from './src/hooks/use-analytics';
18
18
  export { default as useModuleStatus } from './src/hooks/use-module-status';
19
19
  export { default as JetpackEditorPanelLogo } from './src/components/jetpack-editor-panel-logo';
20
+ export { getBlockIconComponent, getBlockIconProp } from './src/get-block-icon-from-metadata';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-shared-extension-utils",
3
- "version": "0.11.5",
3
+ "version": "0.12.1",
4
4
  "description": "Utility functions used by the block editor extensions",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
6
6
  "bugs": {
@@ -18,14 +18,14 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@automattic/jetpack-analytics": "^0.1.27",
21
- "@automattic/jetpack-components": "^0.42.3",
22
- "@automattic/jetpack-connection": "^0.29.10",
23
- "@wordpress/api-fetch": "6.38.0",
24
- "@wordpress/compose": "6.18.0",
25
- "@wordpress/element": "5.18.0",
26
- "@wordpress/i18n": "4.41.0",
27
- "@wordpress/plugins": "6.9.0",
28
- "@wordpress/url": "3.42.0",
21
+ "@automattic/jetpack-components": "^0.43.1",
22
+ "@automattic/jetpack-connection": "^0.30.1",
23
+ "@wordpress/api-fetch": "6.40.0",
24
+ "@wordpress/compose": "6.20.0",
25
+ "@wordpress/element": "5.20.0",
26
+ "@wordpress/i18n": "4.43.0",
27
+ "@wordpress/plugins": "6.11.0",
28
+ "@wordpress/url": "3.44.0",
29
29
  "lodash": "4.17.21"
30
30
  },
31
31
  "devDependencies": {
@@ -38,11 +38,11 @@
38
38
  "react-dom": "18.2.0",
39
39
  "jetpack-js-tools": "workspace:*",
40
40
  "@automattic/jetpack-webpack-config": "workspace:*",
41
- "@wordpress/babel-plugin-import-jsx-pragma": "4.24.0",
42
41
  "@testing-library/dom": "8.19.1",
43
42
  "@testing-library/react": "13.4.0",
44
43
  "@testing-library/user-event": "14.4.3",
45
- "@babel/plugin-transform-react-jsx": "7.22.15"
44
+ "@babel/plugin-transform-react-jsx": "7.22.15",
45
+ "@wordpress/babel-plugin-import-jsx-pragma": "4.26.0"
46
46
  },
47
47
  "exports": {
48
48
  ".": "./index.js"
@@ -0,0 +1,56 @@
1
+ /* eslint-disable jsdoc/no-undefined-types */
2
+ import { createElement } from '@wordpress/element';
3
+ import getIconColor from './get-icon-color';
4
+
5
+ /**
6
+ * Generate an icon as a React component from the SVG markup defined in a block.json metadata file.
7
+ * This prevents us from duplicating the markup in various places.
8
+ *
9
+ * Note: using an `img` tag and passing the SVG markup as a data URI doesn't allow us to
10
+ * dynamically set the icon color later on.
11
+ *
12
+ * @param {object} metadata - Block.json content
13
+ * @returns {React.Component} Icon component
14
+ */
15
+ export function getBlockIconComponent( metadata ) {
16
+ // Set default values
17
+ const attrs = {};
18
+ let tagName = 'span';
19
+ let markup = metadata.icon;
20
+
21
+ // Convert SVG from string to HTML element
22
+ const placeholder = document.createElement( 'div' );
23
+ placeholder.innerHTML = metadata.icon;
24
+ const svg = placeholder.querySelector( 'svg' );
25
+
26
+ // Get SVG attributes and content
27
+ if ( svg ) {
28
+ tagName = 'svg';
29
+ markup = svg.innerHTML;
30
+
31
+ Array.from( svg.attributes ).forEach(
32
+ ( { nodeName, nodeValue } ) => ( attrs[ nodeName ] = nodeValue )
33
+ );
34
+ }
35
+
36
+ return createElement( tagName, {
37
+ ...attrs,
38
+ dangerouslySetInnerHTML: { __html: markup || '' },
39
+ } );
40
+ }
41
+
42
+ /**
43
+ * A block icon needs to be redefined on the front end as a React component, since a string - even
44
+ * SVG markup - is interpreted as a dashicon. This function returns the object that must be passed
45
+ * to the `icon` attribute when registering the block in the front end. It also sets the color
46
+ * of the icon.
47
+ *
48
+ * @param {object} metadata - Block.json content
49
+ * @returns {object} Icon property for client registration
50
+ */
51
+ export function getBlockIconProp( metadata ) {
52
+ return {
53
+ src: getBlockIconComponent( metadata ),
54
+ foreground: getIconColor(),
55
+ };
56
+ }
@@ -0,0 +1,23 @@
1
+ import { isAtomicSite, isSimpleSite } from './site-type-utils';
2
+
3
+ /**
4
+ * Constants
5
+ */
6
+ const JETPACK_GREEN_40 = '#069e08';
7
+
8
+ /**
9
+ * Returns the icon color for Jetpack blocks.
10
+ *
11
+ * Green in the Jetpack context, otherwise black for Simple sites or Atomic sites.
12
+ *
13
+ * @returns {string} HEX color for block editor icons
14
+ */
15
+ export default function getIconColor() {
16
+ if ( isAtomicSite() || isSimpleSite() ) {
17
+ // Return null to match core block styling
18
+ return null;
19
+ }
20
+
21
+ // Jetpack Green
22
+ return JETPACK_GREEN_40;
23
+ }