ag-common 0.0.291 → 0.0.294

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.
@@ -6,3 +6,4 @@ export declare const convertRemToPixels: (rem: number) => number;
6
6
  * @returns
7
7
  */
8
8
  export declare const filterDataProps: (p: any) => Record<string, string>;
9
+ export declare const isRightClick: (event: MouseEvent | TouchEvent) => boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterDataProps = exports.convertRemToPixels = exports.domContains = void 0;
3
+ exports.isRightClick = exports.filterDataProps = exports.convertRemToPixels = exports.domContains = void 0;
4
4
  const array_1 = require("../../common/helpers/array");
5
5
  const domContains = (e, x, y) => {
6
6
  if (!e) {
@@ -30,3 +30,16 @@ const filterDataProps = (p) => {
30
30
  return (0, array_1.arrayToObject)(x, (s) => s[0], (s) => s[1]);
31
31
  };
32
32
  exports.filterDataProps = filterDataProps;
33
+ const isRightClick = (event) => {
34
+ //
35
+ let isRightMB = false;
36
+ if ('which' in event)
37
+ // Gecko (Firefox), WebKit (Safari/Chrome) & Opera
38
+ isRightMB = event.which == 3;
39
+ else if ('button' in event)
40
+ // IE, Opera
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ isRightMB = event.button == 2;
43
+ return isRightMB;
44
+ };
45
+ exports.isRightClick = isRightClick;
@@ -49,6 +49,7 @@ export interface IRequestCommon {
49
49
  url: LocationSubset;
50
50
  lang: TLang;
51
51
  userAgent: string;
52
+ defaultHost: string;
52
53
  }
53
54
  export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialStateCommon {
54
55
  request: TRequest;
@@ -64,7 +65,7 @@ export interface IStateCommon<TRequest extends IRequestCommon> extends IInitialS
64
65
  * @param param0
65
66
  * @returns
66
67
  */
67
- export declare const getClientOrServerReqHref: ({ url: { href, query }, forceServer, userAgent, }: {
68
+ export declare const getClientOrServerReqHref: ({ url: { href, query }, forceServer, userAgent, darkMode, defaultHost, }: {
68
69
  url: {
69
70
  /**
70
71
  * parse querystring keyvalues
@@ -81,9 +82,15 @@ export declare const getClientOrServerReqHref: ({ url: { href, query }, forceSer
81
82
  forceServer?: boolean | undefined;
82
83
  /** will use navigator if possible */
83
84
  userAgent?: string | undefined;
85
+ /** will use window.matchMedia */
86
+ darkMode?: boolean | undefined;
87
+ defaultHost: string;
84
88
  }) => {
85
89
  url: LocationSubset;
86
90
  userAgent: string;
91
+ darkMode: boolean;
92
+ lang: TLang;
93
+ defaultHost: string;
87
94
  };
88
95
  /**
89
96
  * get server side parsed url
@@ -110,6 +117,9 @@ export declare const getServerReq: ({ defaultHost, pathname, query, headers, }:
110
117
  }) => {
111
118
  url: LocationSubset;
112
119
  userAgent: string;
120
+ darkMode: boolean;
121
+ lang: TLang;
122
+ defaultHost: string;
113
123
  };
114
124
  export interface INextCtx {
115
125
  req?: {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getServerReq = exports.getClientOrServerReqHref = void 0;
4
+ const i18n_1 = require("../../common/helpers/i18n");
4
5
  const string_1 = require("../../common/helpers/string");
5
6
  const object_1 = require("../../common/helpers/object");
6
7
  const url_1 = require("url");
@@ -18,22 +19,32 @@ const calculateServerHref = ({ host, pathname, }) => {
18
19
  href += host + pathname;
19
20
  return decodeURIComponent(href);
20
21
  };
22
+ const getRenderLanguage = ({ defaultHost, url, }) => {
23
+ var _a, _b, _c, _d;
24
+ const prefixReg = new RegExp(`(.*?).(local|${defaultHost.toLowerCase()})`, 'gim');
25
+ const host = (_c = (_b = (_a = url.host) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : '';
26
+ const prefix = host.trim().length !== 0 && ((_d = prefixReg.exec(host)) === null || _d === void 0 ? void 0 : _d[1]);
27
+ if (!prefix) {
28
+ return 'en';
29
+ }
30
+ return (0, i18n_1.getValidatedLang)(prefix);
31
+ };
21
32
  /**
22
33
  * get parsed url. use on client/SSR. defaults to window location if possible
23
34
  * @param param0
24
35
  * @returns
25
36
  */
26
- const getClientOrServerReqHref = ({ url: { href, query }, forceServer = false, userAgent, }) => {
27
- if (typeof window !== 'undefined' && !forceServer) {
28
- href = window.location.href;
29
- }
30
- if (typeof navigator !== 'undefined' && !forceServer) {
31
- if (navigator.userAgent) {
32
- userAgent = navigator.userAgent;
37
+ const getClientOrServerReqHref = ({ url: { href, query }, forceServer = false, userAgent, darkMode, defaultHost, }) => {
38
+ if (typeof window !== 'undefined') {
39
+ if (!forceServer) {
40
+ href = window.location.href;
33
41
  }
42
+ darkMode =
43
+ window.matchMedia &&
44
+ window.matchMedia('(prefers-color-scheme: dark)').matches;
34
45
  }
35
- if (!userAgent) {
36
- userAgent = '?';
46
+ if (typeof navigator !== 'undefined') {
47
+ userAgent = navigator.userAgent;
37
48
  }
38
49
  if (!href) {
39
50
  throw new Error('no href');
@@ -49,7 +60,13 @@ const getClientOrServerReqHref = ({ url: { href, query }, forceServer = false, u
49
60
  protocol: parsed.protocol || '',
50
61
  query: Object.assign(Object.assign({}, query), (0, string_1.stringToObject)(parsed.query || '', '=', '&')),
51
62
  };
52
- return { url, userAgent };
63
+ return {
64
+ url,
65
+ userAgent: userAgent !== null && userAgent !== void 0 ? userAgent : '?',
66
+ darkMode: darkMode !== null && darkMode !== void 0 ? darkMode : false,
67
+ lang: getRenderLanguage({ url, defaultHost }),
68
+ defaultHost,
69
+ };
53
70
  };
54
71
  exports.getClientOrServerReqHref = getClientOrServerReqHref;
55
72
  /**
@@ -72,6 +89,7 @@ const getServerReq = ({ defaultHost, pathname, query, headers, }) => {
72
89
  },
73
90
  forceServer: true,
74
91
  userAgent: (_a = headers['user-agent']) === null || _a === void 0 ? void 0 : _a.toLowerCase(),
92
+ defaultHost,
75
93
  });
76
94
  return ret;
77
95
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useOnClickOutside = void 0;
4
+ const dom_1 = require("./dom");
4
5
  const react_1 = require("react");
5
6
  function useOnClickOutside(p, handler) {
6
7
  (0, react_1.useEffect)(() => {
@@ -9,6 +10,11 @@ function useOnClickOutside(p, handler) {
9
10
  }
10
11
  const listener = (event) => {
11
12
  var _a;
13
+ //
14
+ const isRightMB = (0, dom_1.isRightClick)(event);
15
+ if (isRightMB) {
16
+ return;
17
+ }
12
18
  const el = (_a = p.ref) === null || _a === void 0 ? void 0 : _a.current;
13
19
  // Do nothing if clicking ref's element or descendent elements
14
20
  if (!el || el.contains((event === null || event === void 0 ? void 0 : event.target) || null)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.291",
3
+ "version": "0.0.294",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -32,7 +32,7 @@
32
32
  "styled-components": ">=5"
33
33
  },
34
34
  "devDependencies": {
35
- "@babel/core": "7.18.6",
35
+ "@babel/core": "7.18.9",
36
36
  "@storybook/addon-actions": "6.5.9",
37
37
  "@storybook/addon-docs": "6.5.9",
38
38
  "@storybook/addon-essentials": "6.5.9",
@@ -42,14 +42,14 @@
42
42
  "@storybook/react": "6.5.9",
43
43
  "@storybook/theming": "6.5.9",
44
44
  "@types/jsonwebtoken": "8.5.8",
45
- "@types/node": "18.0.3",
45
+ "@types/node": "18.0.6",
46
46
  "@types/react": "18.0.15",
47
47
  "@types/react-dom": "18.0.6",
48
48
  "@types/styled-components": "5.1.25",
49
- "@typescript-eslint/eslint-plugin": "5.30.6",
50
- "@typescript-eslint/parser": "5.30.6",
49
+ "@typescript-eslint/eslint-plugin": "5.30.7",
50
+ "@typescript-eslint/parser": "5.30.7",
51
51
  "cross-env": "7.0.3",
52
- "eslint": "8.19.0",
52
+ "eslint": "8.20.0",
53
53
  "eslint-config-airbnb-typescript": "17.0.0",
54
54
  "eslint-config-prettier": "8.5.0",
55
55
  "eslint-plugin-import": "2.26.0",