newspack-components 3.0.0-alpha.3 → 3.0.0-alpha.4

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.
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _components = require("@wordpress/components");
9
+ var _element = require("@wordpress/element");
10
+ var _router = _interopRequireDefault(require("../proxied-imports/router"));
11
+ require("./style.scss");
12
+ var _jsxRuntime = require("react/jsx-runtime");
13
+ /**
14
+ * Button
15
+ */
16
+
17
+ /**
18
+ * WordPress dependencies.
19
+ */
20
+
21
+ /**
22
+ * Internal dependencies
23
+ */
24
+
25
+ const {
26
+ useHistory
27
+ } = _router.default;
28
+ const Button = ({
29
+ href,
30
+ onClick,
31
+ ...otherProps
32
+ }) => {
33
+ const history = useHistory();
34
+ const [isAwaitingOnClick, setIsAwaitingOnClick] = (0, _element.useState)(false);
35
+
36
+ // If both onClick and href are present, await the onClick action an then redirect.
37
+ if (href && onClick) {
38
+ otherProps.onClick = async () => {
39
+ setIsAwaitingOnClick(true);
40
+ await onClick();
41
+ setIsAwaitingOnClick(false);
42
+ history.push((href || '').replace('#', ''));
43
+ };
44
+ } else {
45
+ otherProps.href = href;
46
+ otherProps.onClick = onClick;
47
+ }
48
+ if (isAwaitingOnClick) {
49
+ otherProps.disabled = true;
50
+ }
51
+ // @ts-ignore - @wordpress/components' Button can only have either href or onClick, not both.
52
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
53
+ ...otherProps
54
+ });
55
+ };
56
+ var _default = exports.default = Button;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ require("./style.scss");
9
+ var _classnames = _interopRequireDefault(require("classnames"));
10
+ var _jsxRuntime = require("react/jsx-runtime");
11
+ /**
12
+ * Grid
13
+ */
14
+
15
+ /**
16
+ * Internal dependencies
17
+ */
18
+
19
+ /**
20
+ * External dependencies
21
+ */
22
+
23
+ const Grid = ({
24
+ className = '',
25
+ columns = 2,
26
+ gutter = 32,
27
+ noMargin = false,
28
+ rowGap = 0,
29
+ ...otherProps
30
+ }) => {
31
+ const classes = (0, _classnames.default)('newspack-grid', noMargin && 'newspack-grid--no-margin', columns && 'newspack-grid__columns-' + columns, gutter && 'newspack-grid__gutter-' + gutter, rowGap && 'newspack-grid__row-gap-' + rowGap, className);
32
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
33
+ className: classes,
34
+ ...otherProps
35
+ });
36
+ };
37
+ var _default = exports.default = Grid;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _element = require("@wordpress/element");
9
+ var _usePrompt = _interopRequireDefault(require("./usePrompt"));
10
+ var _useObjectState = _interopRequireDefault(require("./useObjectState"));
11
+ var _useOnClickOutside = _interopRequireDefault(require("./useOnClickOutside"));
12
+ /**
13
+ * WordPress dependencies
14
+ */
15
+
16
+ const useUniqueId = prefix => {
17
+ const id = (0, _element.useRef)(`${prefix}-${Math.round(Math.random() * 99999)}`);
18
+ return id.current;
19
+ };
20
+ var _default = exports.default = {
21
+ usePrompt: _usePrompt.default,
22
+ useObjectState: _useObjectState.default,
23
+ useOnClickOutside: _useOnClickOutside.default,
24
+ useUniqueId
25
+ };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _element = require("@wordpress/element");
9
+ var _router = _interopRequireDefault(require("../proxied-imports/router"));
10
+ // https://gist.github.com/sibelius/60a4e11da1f826b8d60dc3975a1ac805
11
+
12
+ /**
13
+ * External dependencies.
14
+ */
15
+
16
+ /**
17
+ * Internal dependencies.
18
+ */
19
+
20
+ const {
21
+ useHistory
22
+ } = _router.default;
23
+ var _default = (when, message) => {
24
+ const history = useHistory();
25
+ const self = (0, _element.useRef)(null);
26
+ const onWindowOrTabClose = event => {
27
+ if (!when) {
28
+ return;
29
+ }
30
+ if (typeof event === 'undefined') {
31
+ event = window.event;
32
+ }
33
+ return message;
34
+ };
35
+ (0, _element.useEffect)(() => {
36
+ if (when) {
37
+ self.current = history.block(message);
38
+ window.addEventListener('beforeunload', onWindowOrTabClose);
39
+ }
40
+ return () => {
41
+ if (self.current) {
42
+ self.current();
43
+ }
44
+ window.removeEventListener('beforeunload', onWindowOrTabClose);
45
+ };
46
+ }, [message, when]);
47
+ return self.current || (() => {});
48
+ };
49
+ exports.default = _default;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _components = require("@wordpress/components");
9
+ var _element = require("@wordpress/element");
10
+ var _i18n = require("@wordpress/i18n");
11
+ require("./style.scss");
12
+ var _classnames = _interopRequireDefault(require("classnames"));
13
+ var _jsxRuntime = require("react/jsx-runtime");
14
+ /**
15
+ * Text Control
16
+ */
17
+
18
+ /**
19
+ * WordPress dependencies
20
+ */
21
+
22
+ /**
23
+ * Internal dependencies
24
+ */
25
+
26
+ /**
27
+ * External dependencies
28
+ */
29
+
30
+ const TextControl = ({
31
+ className = '',
32
+ required = false,
33
+ isWide = false,
34
+ withMargin = true,
35
+ ...otherProps
36
+ }) => {
37
+ const wrapperRef = (0, _element.useRef)(null);
38
+ (0, _element.useEffect)(() => {
39
+ if (wrapperRef.current === null) {
40
+ return;
41
+ }
42
+ const labelEl = wrapperRef.current.querySelector('label');
43
+ if (labelEl) {
44
+ labelEl.setAttribute('data-required-text', (0, _i18n.__)('(required)', 'newspack-plugin'));
45
+ }
46
+ }, [wrapperRef.current]);
47
+ const classes = (0, _classnames.default)('newspack-text-control', {
48
+ 'newspack-text-control--wide': isWide,
49
+ 'newspack-text-control--with-margin': withMargin
50
+ }, className);
51
+ return required ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
52
+ ref: wrapperRef,
53
+ className: "newspack-text-control--required",
54
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
55
+ className: classes,
56
+ required: required,
57
+ ...otherProps
58
+ })
59
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
60
+ className: classes,
61
+ ...otherProps
62
+ });
63
+ };
64
+ var _default = exports.default = TextControl;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Button
3
+ */
4
+
5
+ /**
6
+ * WordPress dependencies.
7
+ */
8
+ import { Button as BaseComponent } from '@wordpress/components';
9
+ import { useState } from '@wordpress/element';
10
+
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+ import Router from '../proxied-imports/router';
15
+ import './style.scss';
16
+ import { jsx as _jsx } from "react/jsx-runtime";
17
+ const {
18
+ useHistory
19
+ } = Router;
20
+ const Button = ({
21
+ href,
22
+ onClick,
23
+ ...otherProps
24
+ }) => {
25
+ const history = useHistory();
26
+ const [isAwaitingOnClick, setIsAwaitingOnClick] = useState(false);
27
+
28
+ // If both onClick and href are present, await the onClick action an then redirect.
29
+ if (href && onClick) {
30
+ otherProps.onClick = async () => {
31
+ setIsAwaitingOnClick(true);
32
+ await onClick();
33
+ setIsAwaitingOnClick(false);
34
+ history.push((href || '').replace('#', ''));
35
+ };
36
+ } else {
37
+ otherProps.href = href;
38
+ otherProps.onClick = onClick;
39
+ }
40
+ if (isAwaitingOnClick) {
41
+ otherProps.disabled = true;
42
+ }
43
+ // @ts-ignore - @wordpress/components' Button can only have either href or onClick, not both.
44
+ return /*#__PURE__*/_jsx(BaseComponent, {
45
+ ...otherProps
46
+ });
47
+ };
48
+ export default Button;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Grid
3
+ */
4
+
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import './style.scss';
9
+
10
+ /**
11
+ * External dependencies
12
+ */
13
+ import classnames from 'classnames';
14
+ import { jsx as _jsx } from "react/jsx-runtime";
15
+ const Grid = ({
16
+ className = '',
17
+ columns = 2,
18
+ gutter = 32,
19
+ noMargin = false,
20
+ rowGap = 0,
21
+ ...otherProps
22
+ }) => {
23
+ const classes = classnames('newspack-grid', noMargin && 'newspack-grid--no-margin', columns && 'newspack-grid__columns-' + columns, gutter && 'newspack-grid__gutter-' + gutter, rowGap && 'newspack-grid__row-gap-' + rowGap, className);
24
+ return /*#__PURE__*/_jsx("div", {
25
+ className: classes,
26
+ ...otherProps
27
+ });
28
+ };
29
+ export default Grid;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useRef } from '@wordpress/element';
5
+ import usePrompt from './usePrompt';
6
+ import useObjectState from './useObjectState';
7
+ import useOnClickOutside from './useOnClickOutside';
8
+ const useUniqueId = prefix => {
9
+ const id = useRef(`${prefix}-${Math.round(Math.random() * 99999)}`);
10
+ return id.current;
11
+ };
12
+ export default {
13
+ usePrompt,
14
+ useObjectState,
15
+ useOnClickOutside,
16
+ useUniqueId
17
+ };
@@ -0,0 +1,40 @@
1
+ // https://gist.github.com/sibelius/60a4e11da1f826b8d60dc3975a1ac805
2
+
3
+ /**
4
+ * External dependencies.
5
+ */
6
+ import { useEffect, useRef } from '@wordpress/element';
7
+
8
+ /**
9
+ * Internal dependencies.
10
+ */
11
+ import Router from '../proxied-imports/router';
12
+ const {
13
+ useHistory
14
+ } = Router;
15
+ export default ((when, message) => {
16
+ const history = useHistory();
17
+ const self = useRef(null);
18
+ const onWindowOrTabClose = event => {
19
+ if (!when) {
20
+ return;
21
+ }
22
+ if (typeof event === 'undefined') {
23
+ event = window.event;
24
+ }
25
+ return message;
26
+ };
27
+ useEffect(() => {
28
+ if (when) {
29
+ self.current = history.block(message);
30
+ window.addEventListener('beforeunload', onWindowOrTabClose);
31
+ }
32
+ return () => {
33
+ if (self.current) {
34
+ self.current();
35
+ }
36
+ window.removeEventListener('beforeunload', onWindowOrTabClose);
37
+ };
38
+ }, [message, when]);
39
+ return self.current || (() => {});
40
+ });
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Text Control
3
+ */
4
+
5
+ /**
6
+ * WordPress dependencies
7
+ */
8
+ import { TextControl as BaseComponent } from '@wordpress/components';
9
+ import { useEffect, useRef } from '@wordpress/element';
10
+ import { __ } from '@wordpress/i18n';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import './style.scss';
16
+
17
+ /**
18
+ * External dependencies
19
+ */
20
+ import classNames from 'classnames';
21
+ import { jsx as _jsx } from "react/jsx-runtime";
22
+ const TextControl = ({
23
+ className = '',
24
+ required = false,
25
+ isWide = false,
26
+ withMargin = true,
27
+ ...otherProps
28
+ }) => {
29
+ const wrapperRef = useRef(null);
30
+ useEffect(() => {
31
+ if (wrapperRef.current === null) {
32
+ return;
33
+ }
34
+ const labelEl = wrapperRef.current.querySelector('label');
35
+ if (labelEl) {
36
+ labelEl.setAttribute('data-required-text', __('(required)', 'newspack-plugin'));
37
+ }
38
+ }, [wrapperRef.current]);
39
+ const classes = classNames('newspack-text-control', {
40
+ 'newspack-text-control--wide': isWide,
41
+ 'newspack-text-control--with-margin': withMargin
42
+ }, className);
43
+ return required ? /*#__PURE__*/_jsx("div", {
44
+ ref: wrapperRef,
45
+ className: "newspack-text-control--required",
46
+ children: /*#__PURE__*/_jsx(BaseComponent, {
47
+ className: classes,
48
+ required: required,
49
+ ...otherProps
50
+ })
51
+ }) : /*#__PURE__*/_jsx(BaseComponent, {
52
+ className: classes,
53
+ ...otherProps
54
+ });
55
+ };
56
+ export default TextControl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "newspack-components",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.4",
4
4
  "description": "Newspack design system components",
5
5
  "author": "Automattic",
6
6
  "license": "GPL-2.0-or-later",
@@ -50,9 +50,10 @@
50
50
  },
51
51
  "scripts": {
52
52
  "clean": "rm -rf dist && rm -rf shared",
53
- "compile-cjs": "npx babel src --config-file ./babel.config.cjs.js --out-dir dist/cjs",
54
- "compile-esm": "npx babel src --out-dir dist/esm",
55
- "prepublishOnly": "npm run clean && npm run compile-esm && npm run compile-cjs && node copy-styles.js",
53
+ "compile:cjs": "npx babel src --config-file ./babel.config.cjs.js --extensions='.js,.jsx,.ts,.tsx' --out-dir dist/cjs",
54
+ "compile:esm": "npx babel src --extensions='.js,.jsx,.ts,.tsx' --out-dir dist/esm",
55
+ "compile": "npm run compile:cjs && npm run compile:esm",
56
+ "prepublishOnly": "npm run clean && npm run compile && node copy-styles.js",
56
57
  "postpublish": "npm run clean && rm -rf node_modules"
57
58
  }
58
59
  }