@pushwoosh/dumb-components 1.1.218 → 1.1.219

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.
@@ -1,27 +1,37 @@
1
1
  import { Picker } from 'emoji-mart';
2
2
  import { createElement, useEffect, useRef } from 'react';
3
+ import { useThemeName } from '../hooks';
3
4
  async function data() {
4
5
  const response = await fetch('https://cdn.jsdelivr.net/npm/@emoji-mart/data');
5
6
  return response.json();
6
7
  }
7
- export const EmojiPicker = props => {
8
+ export const EmojiPicker = ({
9
+ theme,
10
+ ...props
11
+ }) => {
8
12
  const ref = useRef(null);
9
13
  const instance = useRef(null);
14
+ const themeOfBuiltPicker = useRef(null);
15
+ const observedTheme = useThemeName(ref, !theme);
16
+ const pickerTheme = theme ?? observedTheme;
10
17
  useEffect(() => {
11
- if (instance.current) {
18
+ const isThemeAlreadyBuilt = instance.current && themeOfBuiltPicker.current === pickerTheme;
19
+ if (isThemeAlreadyBuilt) {
12
20
  instance.current.update({
13
21
  data,
14
22
  ...props
15
23
  });
16
- } else {
17
- const PickerElement = window?.customElements.get('em-emoji-picker');
18
- instance.current = new (PickerElement || Picker)({
19
- data,
20
- ...props,
21
- ref
22
- });
24
+ return;
23
25
  }
24
- }, [props]);
26
+ const PickerElement = window?.customElements.get('em-emoji-picker');
27
+ instance.current = new (PickerElement || Picker)({
28
+ data,
29
+ ...props,
30
+ theme: pickerTheme,
31
+ ref
32
+ });
33
+ themeOfBuiltPicker.current = pickerTheme;
34
+ }, [props, pickerTheme]);
25
35
  return createElement('div', {
26
36
  ref
27
37
  });
@@ -3,5 +3,6 @@ export type EmojiPickerProps = {
3
3
  onEmojiSelect: (emoji: {
4
4
  native: string;
5
5
  }) => void;
6
+ /** Pin the picker to one palette; omit it to follow the theme of the surface it opens on. */
6
7
  theme?: 'light' | 'dark';
7
8
  };
@@ -0,0 +1,2 @@
1
+ /** Splits a hex colour into the bare `r, g, b` channels that `rgb(var(--…))` needs. */
2
+ export declare function rgbChannels(hex: string): string;
@@ -0,0 +1,5 @@
1
+ /** Splits a hex colour into the bare `r, g, b` channels that `rgb(var(--…))` needs. */
2
+ export function rgbChannels(hex) {
3
+ const value = parseInt(hex.replace('#', '').slice(0, 6), 16);
4
+ return [value >> 16 & 255, value >> 8 & 255, value & 255].join(', ');
5
+ }
@@ -1,15 +1,21 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useRef } from 'react';
2
3
  import { EditorEmojiPickerStyled } from './EditorEmojiPicker.styled';
3
4
  import { EmojiPicker } from '../EmojiPicker';
5
+ import { useThemeName } from '../hooks';
4
6
  export const EditorEmojiPicker = ({
5
7
  onEmojiSelect
6
8
  }) => {
9
+ const ref = useRef(null);
10
+ const theme = useThemeName(ref);
7
11
  return _jsx(EditorEmojiPickerStyled, {
12
+ ref: ref,
13
+ "$theme": theme,
8
14
  children: _jsx(EmojiPicker, {
15
+ theme: theme,
9
16
  onEmojiSelect: emoji => {
10
17
  onEmojiSelect(emoji.native);
11
- },
12
- theme: "light"
18
+ }
13
19
  })
14
20
  });
15
21
  };
@@ -1 +1,4 @@
1
- export declare const EditorEmojiPickerStyled: import("styled-components").StyledComponent<"div", any, {}, never>;
1
+ import { type ThemeName } from '@pushwoosh/kit-constants';
2
+ export declare const EditorEmojiPickerStyled: import("styled-components").StyledComponent<"div", any, {
3
+ $theme: ThemeName;
4
+ }, never>;
@@ -1,6 +1,9 @@
1
1
  import styled from 'styled-components';
2
- import { Color, Shadow, ShapeRadius } from '@pushwoosh/kit-constants';
2
+ import { Color, getThemeColors, Shadow, ShapeRadius } from '@pushwoosh/kit-constants';
3
+ import { rgbChannels } from './EditorEmojiPicker.helpers';
3
4
  export const EditorEmojiPickerStyled = styled.div.withConfig({
4
5
  displayName: "EditorEmojiPickerStyled",
5
6
  componentId: "sc-sdjrip-0"
6
- })(["min-width:352px;em-emoji-picker{border:1px solid ", ";--border-radius:", ";--shadow:", ";background-color:", ";--rgb-background:", ";}"], Color.FORM, ShapeRadius.CONTROL, Shadow.REGULAR, Color.FROZEN, Color.FROZEN);
7
+ })(["min-width:352px;em-emoji-picker{border:1px solid ", ";--border-radius:", ";--shadow:", ";background-color:", ";--rgb-background:", ";}"], Color.FORM, ShapeRadius.CONTROL, Shadow.REGULAR, Color.FROZEN, ({
8
+ $theme
9
+ }) => rgbChannels(getThemeColors($theme).FROZEN));
package/hooks/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './useOnClickOutside';
2
2
  export * from './useElementWidth';
3
3
  export * from './useLayer';
4
+ export * from './useThemeName';
package/hooks/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './useOnClickOutside';
2
2
  export * from './useElementWidth';
3
- export * from './useLayer';
3
+ export * from './useLayer';
4
+ export * from './useThemeName';
@@ -0,0 +1,6 @@
1
+ import { type RefObject } from 'react';
2
+ import { type ThemeName } from '@pushwoosh/kit-constants';
3
+ /** Current theme as a value, for widgets that cannot read `var(--pw-color-*)` — canvas, shadow DOM.
4
+ * @param ref - element inside the theme scope, so a light island wins over the document theme
5
+ * @param isEnabled - pass `false` when the caller already knows the theme and needs no observer */
6
+ export declare function useThemeName(ref?: RefObject<Element>, isEnabled?: boolean): ThemeName;
@@ -0,0 +1,30 @@
1
+ import { useLayoutEffect, useState } from 'react';
2
+ import { DEFAULT_THEME, THEME_ATTRIBUTE } from '@pushwoosh/kit-constants';
3
+ function readThemeName(element) {
4
+ if (typeof document === 'undefined') {
5
+ return DEFAULT_THEME;
6
+ }
7
+ const scope = element?.closest(`[${THEME_ATTRIBUTE}]`) ?? document.documentElement;
8
+ return scope.getAttribute(THEME_ATTRIBUTE) === 'dark' ? 'dark' : DEFAULT_THEME;
9
+ }
10
+ /** Current theme as a value, for widgets that cannot read `var(--pw-color-*)` — canvas, shadow DOM.
11
+ * @param ref - element inside the theme scope, so a light island wins over the document theme
12
+ * @param isEnabled - pass `false` when the caller already knows the theme and needs no observer */
13
+ export function useThemeName(ref, isEnabled = true) {
14
+ const [themeName, setThemeName] = useState(() => readThemeName(ref?.current ?? null));
15
+ useLayoutEffect(() => {
16
+ if (!isEnabled) {
17
+ return undefined;
18
+ }
19
+ const syncThemeName = () => setThemeName(readThemeName(ref?.current ?? null));
20
+ syncThemeName();
21
+ const observer = new MutationObserver(syncThemeName);
22
+ observer.observe(document.documentElement, {
23
+ attributes: true,
24
+ attributeFilter: [THEME_ATTRIBUTE],
25
+ subtree: true
26
+ });
27
+ return () => observer.disconnect();
28
+ }, [ref, isEnabled]);
29
+ return themeName;
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/dumb-components",
3
- "version": "1.1.218",
3
+ "version": "1.1.219",
4
4
  "description": "React components to build Pushwoosh products",
5
5
  "main": "index.js",
6
6
  "module": "index.js",