@cyber-harbour/ui 1.0.60 → 1.0.62

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,6 +1,7 @@
1
1
  import { StyleSheetManager, ThemeProvider as ThemeProviderStyled, WebTarget } from 'styled-components';
2
2
  import { lightTheme, darkTheme } from './themes';
3
3
  import { GlobalStyle } from './GlobalStyle';
4
+ import { BreakpointProvider } from './Breakpoint';
4
5
 
5
6
  interface ThemeProviderProps {
6
7
  children: any;
@@ -12,8 +13,10 @@ export const ThemeProvider = ({ children, mode }: ThemeProviderProps) => {
12
13
  return (
13
14
  <StyleSheetManager shouldForwardProp={shouldForwardProp}>
14
15
  <ThemeProviderStyled theme={mode == 'light' || mode === 'LIGHT' ? lightTheme : darkTheme}>
15
- <GlobalStyle />
16
- {children}
16
+ <BreakpointProvider>
17
+ <GlobalStyle />
18
+ {children}
19
+ </BreakpointProvider>
17
20
  </ThemeProviderStyled>
18
21
  </StyleSheetManager>
19
22
  );
@@ -1,5 +1,9 @@
1
- import styled, { css, IStyledComponent, DefaultTheme, WebTarget } from 'styled-components';
1
+ import styled, { css, DefaultTheme } from 'styled-components';
2
2
  import { pxToRem } from './utils';
3
+ import React, { useMemo } from 'react';
4
+ import { useBreakpoint } from './Breakpoint';
5
+ import { Breakpoint } from './types';
6
+ import { breakpoints } from './themes/config';
3
7
 
4
8
  type SpaceProps = {
5
9
  m?: string | number; // margin
@@ -18,9 +22,39 @@ type SpaceProps = {
18
22
  py?: string | number; // padding-top + padding-bottom
19
23
  };
20
24
 
21
- export type FabricComponent<T = object> = T & SpaceProps;
25
+ type GeneratedFabricMarginProperties =
26
+ | 'margin'
27
+ | 'margin-top'
28
+ | 'margin-right'
29
+ | 'margin-bottom'
30
+ | 'margin-left'
31
+ | 'margin-inline'
32
+ | 'margin-block'
33
+ | 'padding'
34
+ | 'padding-top'
35
+ | 'padding-right'
36
+ | 'padding-bottom'
37
+ | 'padding-left'
38
+ | 'padding-inline'
39
+ | 'padding-block';
40
+
41
+ type MediaProps<T = object> = {
42
+ media?: {
43
+ [key in Breakpoint]?: Partial<T>;
44
+ };
45
+ };
22
46
 
23
- const marginStyles = ({ ignoreStyles }: FabricComponentOptions = {}) =>
47
+ type FabricStyledComponentOptions = {
48
+ ignoreStyles?: GeneratedFabricMarginProperties[] | undefined; // Ignore margin styles
49
+ };
50
+
51
+ type CreatedFabricComponent<T extends FabricComponent> =
52
+ | React.ComponentType<Omit<T, 'media'>>
53
+ | React.ForwardRefExoticComponent<Omit<T, 'media'>>;
54
+ export type FabricComponent<T = object> = T & SpaceProps & MediaProps<T>;
55
+ export type StyledFabricComponent<T = object> = T & SpaceProps;
56
+
57
+ const marginStyles = ({ ignoreStyles }: FabricStyledComponentOptions = {}) =>
24
58
  css<FabricComponent>(({ theme, ...props }) => {
25
59
  return `
26
60
  ${generatePropertySpaceStyle(theme, 'margin', props.m, ignoreStyles)}
@@ -33,7 +67,7 @@ const marginStyles = ({ ignoreStyles }: FabricComponentOptions = {}) =>
33
67
  `;
34
68
  });
35
69
 
36
- const paddingStyles = ({ ignoreStyles }: FabricComponentOptions = {}) =>
70
+ const paddingStyles = ({ ignoreStyles }: FabricStyledComponentOptions = {}) =>
37
71
  css<FabricComponent>(({ theme, ...props }) => {
38
72
  return `
39
73
  ${generatePropertySpaceStyle(theme, 'padding', props.p, ignoreStyles)}
@@ -46,21 +80,17 @@ const paddingStyles = ({ ignoreStyles }: FabricComponentOptions = {}) =>
46
80
  `;
47
81
  });
48
82
 
49
- type GeneratedFabricMarginProperties =
50
- | 'margin'
51
- | 'margin-top'
52
- | 'margin-right'
53
- | 'margin-bottom'
54
- | 'margin-left'
55
- | 'margin-inline'
56
- | 'margin-block'
57
- | 'padding'
58
- | 'padding-top'
59
- | 'padding-right'
60
- | 'padding-bottom'
61
- | 'padding-left'
62
- | 'padding-inline'
63
- | 'padding-block';
83
+ export const destructSpaceProps = <T extends FabricComponent>(props: T): SpaceProps => {
84
+ const rest: SpaceProps = {};
85
+ ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'p', 'pt', 'pr', 'pb', 'pl', 'px', 'py'].forEach((key) => {
86
+ if (key in props) {
87
+ rest[key as keyof SpaceProps] = props[key as keyof SpaceProps];
88
+ delete props[key as keyof SpaceProps];
89
+ }
90
+ });
91
+
92
+ return rest;
93
+ };
64
94
 
65
95
  export const generatePropertySpaceStyle = (
66
96
  theme: DefaultTheme,
@@ -78,28 +108,52 @@ export const generatePropertySpaceStyle = (
78
108
  return '';
79
109
  };
80
110
 
81
- type FabricComponentOptions = {
82
- ignoreStyles?: GeneratedFabricMarginProperties[] | undefined; // Ignore margin styles
111
+ export function getResponsiveProps<T = object>(
112
+ { media, ...props }: T & MediaProps<T>,
113
+ currentBreakpoint: Breakpoint,
114
+ breakpointOrder: Breakpoint[]
115
+ ) {
116
+ if (!media) {
117
+ return props;
118
+ }
119
+
120
+ // Отримуємо індекс поточного breakpoint
121
+ const currentIndex = breakpointOrder.indexOf(currentBreakpoint);
122
+
123
+ // Створюємо результуючий об'єкт, починаючи з базових props
124
+ let result = { ...props };
125
+
126
+ // Застосовуємо стилі від найменшого breakpoint до поточного (mobile-first)
127
+ for (let i = 0; i <= currentIndex; i++) {
128
+ const breakpoint = breakpointOrder[i];
129
+ if (media[breakpoint]) {
130
+ result = {
131
+ ...result,
132
+ ...media[breakpoint],
133
+ };
134
+ }
135
+ }
136
+
137
+ return result;
138
+ }
139
+
140
+ export const createComponent = <T extends FabricComponent, R = unknown>(Component: CreatedFabricComponent<T>) => {
141
+ return React.forwardRef<R, FabricComponent<T>>((props, ref) => {
142
+ const { currentBreakpoint, breakpointsOrder } = useBreakpoint();
143
+ const responsiveProps = useMemo(
144
+ () => getResponsiveProps<T>(props as T & MediaProps<T>, currentBreakpoint, breakpointsOrder),
145
+ [props, currentBreakpoint]
146
+ );
147
+ return <Component {...responsiveProps} ref={ref} />;
148
+ });
83
149
  };
84
150
 
85
- export const createComponent = <T extends object = {}>(
151
+ export const createStyledComponent = <T extends object = StyledFabricComponent>(
86
152
  component: React.ComponentType<T>,
87
- options?: FabricComponentOptions
153
+ options?: FabricStyledComponentOptions
88
154
  ) => {
89
- return styled(component)<T & FabricComponent>`
155
+ return styled(component)<StyledFabricComponent<T>>`
90
156
  ${marginStyles(options)}
91
157
  ${paddingStyles(options)}
92
158
  `;
93
159
  };
94
-
95
- export const destructSpaceProps = <T extends FabricComponent>(props: T): SpaceProps => {
96
- const rest: SpaceProps = {};
97
- ['m', 'mt', 'mr', 'mb', 'ml', 'mx', 'my', 'p', 'pt', 'pr', 'pb', 'pl', 'px', 'py'].forEach((key) => {
98
- if (key in props) {
99
- rest[key as keyof SpaceProps] = props[key as keyof SpaceProps];
100
- delete props[key as keyof SpaceProps];
101
- }
102
- });
103
-
104
- return rest;
105
- };
@@ -5,3 +5,4 @@ export * from './types';
5
5
  export * from './themes';
6
6
  export * from './componentFabric';
7
7
  export * from './useTheme';
8
+ export * from './Breakpoint';