@soyfri/shared-library 2.0.0-beta.2 → 2.0.0-beta.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.
- package/.dockerignore +8 -0
- package/.github/workflows/publish.yml +107 -0
- package/.prettierrc +3 -0
- package/.storybook/main.ts +19 -0
- package/.storybook/preview.ts +14 -0
- package/.storybook/vitest.setup.ts +9 -0
- package/Dockerfile +37 -0
- package/build.js +102 -0
- package/chromatic.config.json +5 -0
- package/cleanDirectories.js +40 -0
- package/dist/README.md +243 -0
- package/dist/components/Icon/Icon.js +1 -1
- package/dist/components/Table/Table.js +1 -1
- package/dist/index.cjs +24 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +7 -1
- package/dist/mui.d.ts +1 -0
- package/dist/package.json +197 -0
- package/package.json +4 -32
- package/rollup.config.cjs +87 -0
- package/src/components/ActionMenu/ActionMenu.stories.tsx +230 -0
- package/src/components/ActionMenu/ActionMenu.tsx +174 -0
- package/src/components/ActionMenu/index.ts +2 -0
- package/src/components/AppBar/AppBar.stories.tsx +272 -0
- package/src/components/AppBar/AppBar.sx.ts +32 -0
- package/src/components/AppBar/AppBar.tsx +123 -0
- package/src/components/AppBar/AppBarBrand.tsx +120 -0
- package/src/components/AppBar/AppBarContext.ts +25 -0
- package/src/components/AppBar/AppBarMenuToggle.tsx +90 -0
- package/src/components/AppBar/AppBarUserMenu.tsx +217 -0
- package/src/components/AppBar/index.ts +25 -0
- package/src/components/Autocomplete/Autocomplete.definitions.ts +477 -0
- package/src/components/Autocomplete/Autocomplete.helpers.ts +60 -0
- package/src/components/Autocomplete/Autocomplete.stories.tsx +748 -0
- package/src/components/Autocomplete/Autocomplete.sx.ts +30 -0
- package/src/components/Autocomplete/Autocomplete.tsx +361 -0
- package/src/components/Autocomplete/Autocomplete.types.ts +13 -0
- package/src/components/Autocomplete/_parts/AutocompleteChips.tsx +55 -0
- package/src/components/Autocomplete/_parts/AutocompleteLoader.tsx +17 -0
- package/src/components/Autocomplete/_parts/AutocompleteOption.tsx +31 -0
- package/src/components/Autocomplete/index.ts +12 -0
- package/src/components/Avatar/Avatar.definitions.ts +162 -0
- package/src/components/Avatar/Avatar.stories.tsx +258 -0
- package/src/components/Avatar/Avatar.tsx +206 -0
- package/src/components/Avatar/index.ts +1 -0
- package/src/components/Button/Button.definition.ts +97 -0
- package/src/components/Button/Button.stories.tsx +285 -0
- package/src/components/Button/Button.tsx +67 -0
- package/src/components/Button/index.ts +1 -0
- package/src/components/Card/Card.definition.ts +5 -0
- package/src/components/Card/Card.stories.tsx +221 -0
- package/src/components/Card/Card.sx.ts +104 -0
- package/src/components/Card/Card.tsx +200 -0
- package/src/components/Card/index.ts +9 -0
- package/src/components/Chip/Chip.definitions.ts +167 -0
- package/src/components/Chip/Chip.stories.tsx +265 -0
- package/src/components/Chip/Chip.tsx +61 -0
- package/src/components/Chip/index.ts +1 -0
- package/src/components/Column/Column.tsx +29 -0
- package/src/components/Column/index.ts +1 -0
- package/src/components/DatePicker/DatePicker.definitions.ts +228 -0
- package/src/components/DatePicker/DatePicker.helpers.ts +24 -0
- package/src/components/DatePicker/DatePicker.stories.tsx +309 -0
- package/src/components/DatePicker/DatePicker.sx.ts +33 -0
- package/src/components/DatePicker/DatePicker.tsx +189 -0
- package/src/components/DatePicker/DatePicker.types.ts +10 -0
- package/src/components/DatePicker/index.ts +9 -0
- package/src/components/DateRangePicker/DateRangePicker.definitions.ts +191 -0
- package/src/components/DateRangePicker/DateRangePicker.stories.tsx +252 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +56 -0
- package/src/components/DateRangePicker/index.ts +1 -0
- package/src/components/DateTimePicker/DateTimePicker.definitions.ts +256 -0
- package/src/components/DateTimePicker/DateTimePicker.helpers.ts +38 -0
- package/src/components/DateTimePicker/DateTimePicker.stories.tsx +418 -0
- package/src/components/DateTimePicker/DateTimePicker.sx.ts +30 -0
- package/src/components/DateTimePicker/DateTimePicker.tsx +225 -0
- package/src/components/DateTimePicker/DateTimePicker.types.ts +10 -0
- package/src/components/DateTimePicker/index.ts +9 -0
- package/src/components/Drawer/Drawer.stories.tsx +270 -0
- package/src/components/Drawer/Drawer.sx.ts +106 -0
- package/src/components/Drawer/Drawer.tsx +214 -0
- package/src/components/Drawer/DrawerContext.ts +26 -0
- package/src/components/Drawer/DrawerItem.tsx +110 -0
- package/src/components/Drawer/index.ts +10 -0
- package/src/components/Flyout/Flyout.stories.tsx +282 -0
- package/src/components/Flyout/Flyout.tsx +122 -0
- package/src/components/Flyout/index.ts +1 -0
- package/src/components/Gallery/Gallery.definition.tsx +37 -0
- package/src/components/Gallery/Gallery.stories.tsx +82 -0
- package/src/components/Gallery/Gallery.tsx +118 -0
- package/src/components/Gallery/GalleryLightbox.tsx +170 -0
- package/src/components/Gallery/GalleryMain.tsx +84 -0
- package/src/components/Gallery/GalleryThumbnails.tsx +106 -0
- package/src/components/Gallery/index.ts +1 -0
- package/src/components/Icon/Icon.stories.tsx +121 -0
- package/src/components/Icon/Icon.tsx +175 -0
- package/src/components/Icon/index.ts +2 -0
- package/src/components/Input/Input.definitions.ts +324 -0
- package/src/components/Input/Input.helpers.ts +49 -0
- package/src/components/Input/Input.stories.tsx +499 -0
- package/src/components/Input/Input.sx.ts +42 -0
- package/src/components/Input/Input.tsx +141 -0
- package/src/components/Input/Input.types.ts +10 -0
- package/src/components/Input/index.ts +9 -0
- package/src/components/InputGroup/InputGroup.definitions.ts +158 -0
- package/src/components/InputGroup/InputGroup.stories.tsx +267 -0
- package/src/components/InputGroup/InputGroup.tsx +179 -0
- package/src/components/InputGroup/index.ts +1 -0
- package/src/components/MenuButton/MenuButton.stories.tsx +197 -0
- package/src/components/MenuButton/MenuButton.tsx +100 -0
- package/src/components/MenuButton/index.ts +1 -0
- package/src/components/Modal/Modal.stories.tsx +721 -0
- package/src/components/Modal/Modal.tsx +355 -0
- package/src/components/Modal/ModalBody.tsx +16 -0
- package/src/components/Modal/ModalFooter.tsx +71 -0
- package/src/components/Modal/ModalHeader.tsx +18 -0
- package/src/components/Modal/index.ts +6 -0
- package/src/components/PageLoader/PageLoader.stories.tsx +217 -0
- package/src/components/PageLoader/PageLoader.tsx +96 -0
- package/src/components/PageLoader/index.ts +2 -0
- package/src/components/ScrollTopButton/ScrollTopButton.stories.tsx +158 -0
- package/src/components/ScrollTopButton/ScrollTopButton.tsx +135 -0
- package/src/components/ScrollTopButton/index.ts +8 -0
- package/src/components/ScrollTopButton/scrollToTop.ts +37 -0
- package/src/components/Select/Select.definitions.ts +602 -0
- package/src/components/Select/Select.helpers.ts +71 -0
- package/src/components/Select/Select.stories.tsx +687 -0
- package/src/components/Select/Select.sx.ts +14 -0
- package/src/components/Select/Select.tsx +429 -0
- package/src/components/Select/Select.types.ts +15 -0
- package/src/components/Select/_parts/SelectMenuItem.tsx +40 -0
- package/src/components/Select/_parts/SelectSearchHeader.tsx +51 -0
- package/src/components/Select/_parts/SelectValue.tsx +96 -0
- package/src/components/Select/index.ts +14 -0
- package/src/components/Stat/Stat.stories.tsx +85 -0
- package/src/components/Stat/Stat.tsx +117 -0
- package/src/components/Stat/index.ts +2 -0
- package/src/components/StatusMessage/StatusMessage.stories.tsx +130 -0
- package/src/components/StatusMessage/StatusMessage.tsx +162 -0
- package/src/components/StatusMessage/index.ts +2 -0
- package/src/components/Stepper/Step.tsx +21 -0
- package/src/components/Stepper/Stepper.definition.ts +75 -0
- package/src/components/Stepper/Stepper.stories.tsx +122 -0
- package/src/components/Stepper/Stepper.tsx +75 -0
- package/src/components/Stepper/index.ts +2 -0
- package/src/components/Table/EmptyTable.png +0 -0
- package/src/components/Table/Table.definition.ts +580 -0
- package/src/components/Table/Table.stories.tsx +853 -0
- package/src/components/Table/Table.tsx +495 -0
- package/src/components/Table/data.ts +134 -0
- package/src/components/Table/exportsUtils.ts +195 -0
- package/src/components/Table/index.ts +3 -0
- package/src/components/Table/types.ts +34 -0
- package/src/components/Tabs/Tab.definition.ts +53 -0
- package/src/components/Tabs/Tab.tsx +19 -0
- package/src/components/Tabs/Tabs.stories.tsx +118 -0
- package/src/components/Tabs/Tabs.tsx +99 -0
- package/src/components/Tabs/_tabUtils.tsx +4 -0
- package/src/components/Tabs/index.ts +2 -0
- package/src/components/Timeline/Timeline.definition.ts +43 -0
- package/src/components/Timeline/Timeline.stories.tsx +108 -0
- package/src/components/Timeline/Timeline.tsx +49 -0
- package/src/components/Timeline/TimelineItem.tsx +31 -0
- package/src/components/Timeline/index.ts +2 -0
- package/src/components/Tooltip/Tooltip.stories.tsx +129 -0
- package/src/components/Tooltip/Tooltip.tsx +58 -0
- package/src/components/Tooltip/index.ts +1 -0
- package/src/components/_shared/formField.sx.ts +118 -0
- package/src/components/_shared/resolvePreset.ts +35 -0
- package/src/hooks/ClipBoard/ClipBoard.stories.tsx +168 -0
- package/src/hooks/ClipBoard/ClipBoard.tsx +131 -0
- package/src/hooks/ClipBoard/ClipboardUnifiedDemo.tsx +111 -0
- package/src/hooks/ClipBoard/index.ts +1 -0
- package/src/hooks/Wizard/Wizard.stories.tsx +301 -0
- package/src/hooks/Wizard/WizardContext.tsx +166 -0
- package/src/hooks/Wizard/index.ts +6 -0
- package/src/hooks/Wizard/useWizard.ts +13 -0
- package/src/index.ts +17 -0
- package/src/mui.ts +54 -0
- package/src/styles.css +3 -0
- package/src/theme/componentStyles.ts +47 -0
- package/src/theme/tokens.ts +43 -0
- package/tailwind.config.js +10 -0
- package/tsconfig.json +48 -0
- package/tsup.config.js +41 -0
- package/vite.config.js +132 -0
- package/vitest.config.ts +35 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react';
|
|
2
|
+
import { Box, CircularProgress, Typography, Fade } from '@mui/material';
|
|
3
|
+
import type { SxProps, Theme } from '@mui/material/styles';
|
|
4
|
+
|
|
5
|
+
export interface PageLoaderProps {
|
|
6
|
+
/**
|
|
7
|
+
* Si es true, muestra el loader ocupando toda la ventana (fixed, 100vh/vw)
|
|
8
|
+
* con backdrop. Default: true.
|
|
9
|
+
*/
|
|
10
|
+
fullscreen?: boolean;
|
|
11
|
+
/** Texto bajo el spinner. */
|
|
12
|
+
message?: ReactNode;
|
|
13
|
+
/** Logo/imagen opcional arriba del spinner. */
|
|
14
|
+
logo?: ReactNode;
|
|
15
|
+
/** Tamaño del spinner en px. Default: 48. */
|
|
16
|
+
size?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Control explícito. Si es undefined, el loader se muestra apenas se monta.
|
|
19
|
+
* Útil para fades.
|
|
20
|
+
*/
|
|
21
|
+
open?: boolean;
|
|
22
|
+
/** Color del backdrop (solo fullscreen). Default: usa theme.palette.background.default. */
|
|
23
|
+
backdropColor?: string;
|
|
24
|
+
/** sx del root. */
|
|
25
|
+
sx?: SxProps<Theme>;
|
|
26
|
+
className?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Loader full-page o inline. Reemplaza el `LayoutSplashScreen` de Metronic.
|
|
31
|
+
*
|
|
32
|
+
* ```tsx
|
|
33
|
+
* // Splash de arranque
|
|
34
|
+
* <PageLoader logo={<img src={logo} />} message="Cargando..." />
|
|
35
|
+
*
|
|
36
|
+
* // Loader local dentro de una card
|
|
37
|
+
* <PageLoader fullscreen={false} size={32} />
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function PageLoader({
|
|
41
|
+
fullscreen = true,
|
|
42
|
+
message,
|
|
43
|
+
logo,
|
|
44
|
+
size = 48,
|
|
45
|
+
open = true,
|
|
46
|
+
backdropColor,
|
|
47
|
+
sx,
|
|
48
|
+
className,
|
|
49
|
+
}: PageLoaderProps) {
|
|
50
|
+
const rootSx: SxProps<Theme> = [
|
|
51
|
+
(theme) => ({
|
|
52
|
+
display: 'flex',
|
|
53
|
+
flexDirection: 'column',
|
|
54
|
+
alignItems: 'center',
|
|
55
|
+
justifyContent: 'center',
|
|
56
|
+
gap: 2,
|
|
57
|
+
...(fullscreen
|
|
58
|
+
? {
|
|
59
|
+
position: 'fixed',
|
|
60
|
+
inset: 0,
|
|
61
|
+
zIndex: theme.zIndex.modal + 10,
|
|
62
|
+
backgroundColor:
|
|
63
|
+
backdropColor ?? theme.palette.background.default,
|
|
64
|
+
}
|
|
65
|
+
: {
|
|
66
|
+
width: '100%',
|
|
67
|
+
minHeight: 160,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
...(Array.isArray(sx) ? sx : sx ? [sx] : []),
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
const content = (
|
|
74
|
+
<Box className={className} sx={rootSx} role="status" aria-live="polite">
|
|
75
|
+
{logo && <Box sx={{ mb: 1 }}>{logo}</Box>}
|
|
76
|
+
<CircularProgress size={size} thickness={4} />
|
|
77
|
+
{message && (
|
|
78
|
+
<Typography variant="body2" color="text.secondary">
|
|
79
|
+
{message}
|
|
80
|
+
</Typography>
|
|
81
|
+
)}
|
|
82
|
+
</Box>
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
if (!fullscreen) {
|
|
86
|
+
return open ? content : null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<Fade in={open} timeout={{ enter: 0, exit: 200 }} unmountOnExit>
|
|
91
|
+
{content}
|
|
92
|
+
</Fade>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default PageLoader;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
3
|
+
import { Box, Button, Stack, Typography } from '@mui/material';
|
|
4
|
+
import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward';
|
|
5
|
+
|
|
6
|
+
import { ScrollTopButton } from './ScrollTopButton';
|
|
7
|
+
import { scrollToTop } from './scrollToTop';
|
|
8
|
+
|
|
9
|
+
const meta: Meta<typeof ScrollTopButton> = {
|
|
10
|
+
title: 'Components/ScrollTopButton',
|
|
11
|
+
component: ScrollTopButton,
|
|
12
|
+
tags: ['autodocs'],
|
|
13
|
+
parameters: {
|
|
14
|
+
layout: 'fullscreen',
|
|
15
|
+
docs: {
|
|
16
|
+
description: {
|
|
17
|
+
component:
|
|
18
|
+
'Botón flotante que aparece tras scrollear más de `threshold` px y lleva al tope al click. Reemplaza al `ScrollTopComponent` visible de Metronic. Para el caso imperativo (p.ej. tras una mutación) usa el helper `scrollToTop()`.',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
argTypes: {
|
|
23
|
+
threshold: { control: { type: 'number', min: 0, max: 1000, step: 50 } },
|
|
24
|
+
position: {
|
|
25
|
+
control: 'select',
|
|
26
|
+
options: ['bottom-right', 'bottom-left', 'bottom-center'],
|
|
27
|
+
},
|
|
28
|
+
size: { control: 'select', options: ['small', 'medium', 'large'] },
|
|
29
|
+
color: {
|
|
30
|
+
control: 'select',
|
|
31
|
+
options: ['default', 'primary', 'secondary', 'success', 'warning', 'error'],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export default meta;
|
|
37
|
+
type Story = StoryObj<typeof ScrollTopButton>;
|
|
38
|
+
|
|
39
|
+
const TallContent = () => (
|
|
40
|
+
<Box sx={{ p: 3 }}>
|
|
41
|
+
<Typography variant="h5" gutterBottom>
|
|
42
|
+
Scrollea hacia abajo para que aparezca el botón
|
|
43
|
+
</Typography>
|
|
44
|
+
<Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
|
|
45
|
+
El botón aparece cuando `window.scrollY` supera el `threshold`.
|
|
46
|
+
</Typography>
|
|
47
|
+
{Array.from({ length: 60 }).map((_, idx) => (
|
|
48
|
+
<Typography key={idx} variant="body2" sx={{ mb: 1 }}>
|
|
49
|
+
Línea {idx + 1} — contenido de relleno para simular una página larga.
|
|
50
|
+
</Typography>
|
|
51
|
+
))}
|
|
52
|
+
</Box>
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
export const Default: Story = {
|
|
56
|
+
args: {
|
|
57
|
+
threshold: 300,
|
|
58
|
+
position: 'bottom-right',
|
|
59
|
+
},
|
|
60
|
+
render: (args) => (
|
|
61
|
+
<>
|
|
62
|
+
<TallContent />
|
|
63
|
+
<ScrollTopButton {...args} />
|
|
64
|
+
</>
|
|
65
|
+
),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const BottomLeft: Story = {
|
|
69
|
+
args: {
|
|
70
|
+
threshold: 200,
|
|
71
|
+
position: 'bottom-left',
|
|
72
|
+
},
|
|
73
|
+
render: (args) => (
|
|
74
|
+
<>
|
|
75
|
+
<TallContent />
|
|
76
|
+
<ScrollTopButton {...args} />
|
|
77
|
+
</>
|
|
78
|
+
),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const BottomCenter: Story = {
|
|
82
|
+
args: {
|
|
83
|
+
threshold: 200,
|
|
84
|
+
position: 'bottom-center',
|
|
85
|
+
color: 'primary',
|
|
86
|
+
},
|
|
87
|
+
render: (args) => (
|
|
88
|
+
<>
|
|
89
|
+
<TallContent />
|
|
90
|
+
<ScrollTopButton {...args} />
|
|
91
|
+
</>
|
|
92
|
+
),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const CustomIcon: Story = {
|
|
96
|
+
args: {
|
|
97
|
+
threshold: 200,
|
|
98
|
+
color: 'primary',
|
|
99
|
+
icon: <ArrowUpwardIcon />,
|
|
100
|
+
},
|
|
101
|
+
render: (args) => (
|
|
102
|
+
<>
|
|
103
|
+
<TallContent />
|
|
104
|
+
<ScrollTopButton {...args} />
|
|
105
|
+
</>
|
|
106
|
+
),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const SmallSize: Story = {
|
|
110
|
+
args: {
|
|
111
|
+
size: 'small',
|
|
112
|
+
threshold: 150,
|
|
113
|
+
},
|
|
114
|
+
render: (args) => (
|
|
115
|
+
<>
|
|
116
|
+
<TallContent />
|
|
117
|
+
<ScrollTopButton {...args} />
|
|
118
|
+
</>
|
|
119
|
+
),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export const ImperativeHelper: Story = {
|
|
123
|
+
render: () => (
|
|
124
|
+
<Box sx={{ p: 3 }}>
|
|
125
|
+
<Typography variant="h5" gutterBottom>
|
|
126
|
+
Uso imperativo: <code>scrollToTop()</code>
|
|
127
|
+
</Typography>
|
|
128
|
+
<Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
|
|
129
|
+
Reemplazo directo de <code>ScrollTopComponent.goTop()</code> de Metronic.
|
|
130
|
+
Llamar tras mutaciones / aperturas de modales.
|
|
131
|
+
</Typography>
|
|
132
|
+
<Stack direction="row" spacing={1} sx={{ mb: 3 }}>
|
|
133
|
+
<Button variant="contained" onClick={() => scrollToTop()}>
|
|
134
|
+
scrollToTop() (smooth)
|
|
135
|
+
</Button>
|
|
136
|
+
<Button
|
|
137
|
+
variant="outlined"
|
|
138
|
+
onClick={() => scrollToTop({ behavior: 'auto' })}
|
|
139
|
+
>
|
|
140
|
+
scrollToTop { behavior:'auto' }
|
|
141
|
+
</Button>
|
|
142
|
+
</Stack>
|
|
143
|
+
{Array.from({ length: 50 }).map((_, idx) => (
|
|
144
|
+
<Typography key={idx} variant="body2" sx={{ mb: 1 }}>
|
|
145
|
+
Línea {idx + 1}
|
|
146
|
+
</Typography>
|
|
147
|
+
))}
|
|
148
|
+
</Box>
|
|
149
|
+
),
|
|
150
|
+
parameters: {
|
|
151
|
+
docs: {
|
|
152
|
+
description: {
|
|
153
|
+
story:
|
|
154
|
+
'Demuestra el helper imperativo. Cubre el patrón del legacy: llamar `scrollToTop()` tras una mutación para llevar al usuario a ver el mensaje de confirmación del tope.',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import React, { useEffect, useState, type ReactNode } from 'react';
|
|
2
|
+
import { Fab, Zoom, useTheme } from '@mui/material';
|
|
3
|
+
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
|
|
4
|
+
import type { SxProps, Theme } from '@mui/material/styles';
|
|
5
|
+
|
|
6
|
+
import { resolvePreset } from '../_shared/resolvePreset';
|
|
7
|
+
import { scrollToTop } from './scrollToTop';
|
|
8
|
+
|
|
9
|
+
export type ScrollTopButtonPosition =
|
|
10
|
+
| 'bottom-right'
|
|
11
|
+
| 'bottom-left'
|
|
12
|
+
| 'bottom-center';
|
|
13
|
+
|
|
14
|
+
export interface ScrollTopButtonProps {
|
|
15
|
+
/**
|
|
16
|
+
* Distancia en px que hay que scrollear antes de mostrar el botón.
|
|
17
|
+
* Default: `300`.
|
|
18
|
+
*/
|
|
19
|
+
threshold?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Posición del botón en la pantalla. Default: `'bottom-right'`.
|
|
22
|
+
*/
|
|
23
|
+
position?: ScrollTopButtonPosition;
|
|
24
|
+
/**
|
|
25
|
+
* Elemento scrolleable a observar y al que se hace scrollTop. Si se omite,
|
|
26
|
+
* se usa `window` (caso más común).
|
|
27
|
+
*/
|
|
28
|
+
scrollTarget?: Window | HTMLElement | null;
|
|
29
|
+
/** Tamaño del FAB. Default: `'medium'`. */
|
|
30
|
+
size?: 'small' | 'medium' | 'large';
|
|
31
|
+
/**
|
|
32
|
+
* Color del FAB. Default: `'default'`.
|
|
33
|
+
*/
|
|
34
|
+
color?: 'default' | 'primary' | 'secondary' | 'success' | 'warning' | 'error';
|
|
35
|
+
/** Icono custom. Default: `<KeyboardArrowUpIcon />`. */
|
|
36
|
+
icon?: ReactNode;
|
|
37
|
+
/** Texto accesible (aria-label). Default: `"Subir al inicio"`. */
|
|
38
|
+
ariaLabel?: string;
|
|
39
|
+
/** Preset registrado en `theme.styles.ScrollTopButton`. */
|
|
40
|
+
preset?: string;
|
|
41
|
+
/** sx del Fab. Se compone sobre la posición + preset. */
|
|
42
|
+
sx?: SxProps<Theme>;
|
|
43
|
+
className?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const positionSx: Record<ScrollTopButtonPosition, SxProps<Theme>> = {
|
|
47
|
+
'bottom-right': { position: 'fixed', bottom: 24, right: 24 },
|
|
48
|
+
'bottom-left': { position: 'fixed', bottom: 24, left: 24 },
|
|
49
|
+
'bottom-center': {
|
|
50
|
+
position: 'fixed',
|
|
51
|
+
bottom: 24,
|
|
52
|
+
left: '50%',
|
|
53
|
+
transform: 'translateX(-50%)',
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Botón flotante que aparece tras scrollear más de `threshold` px y, al
|
|
59
|
+
* clickearse, hace scroll suave hasta el tope. Reemplaza al componente
|
|
60
|
+
* visible de `ScrollTopComponent` de Metronic.
|
|
61
|
+
*
|
|
62
|
+
* Para el caso imperativo (llamar `goTop()` tras una mutación), usar el
|
|
63
|
+
* helper `scrollToTop()` directamente.
|
|
64
|
+
*
|
|
65
|
+
* ```tsx
|
|
66
|
+
* <ScrollTopButton threshold={400} position="bottom-right" />
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function ScrollTopButton({
|
|
70
|
+
threshold = 300,
|
|
71
|
+
position = 'bottom-right',
|
|
72
|
+
scrollTarget,
|
|
73
|
+
size = 'medium',
|
|
74
|
+
color = 'default',
|
|
75
|
+
icon,
|
|
76
|
+
ariaLabel = 'Subir al inicio',
|
|
77
|
+
preset,
|
|
78
|
+
sx,
|
|
79
|
+
className,
|
|
80
|
+
}: ScrollTopButtonProps) {
|
|
81
|
+
const theme = useTheme();
|
|
82
|
+
const [visible, setVisible] = useState(false);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
// Si viene scrollTarget explícito, usarlo; si no, window.
|
|
86
|
+
const target: Window | HTMLElement | null =
|
|
87
|
+
scrollTarget ?? (typeof window !== 'undefined' ? window : null);
|
|
88
|
+
if (!target) return;
|
|
89
|
+
|
|
90
|
+
const readScrollTop = () =>
|
|
91
|
+
target === window
|
|
92
|
+
? window.scrollY || document.documentElement.scrollTop
|
|
93
|
+
: (target as HTMLElement).scrollTop;
|
|
94
|
+
|
|
95
|
+
const handleScroll = () => {
|
|
96
|
+
setVisible(readScrollTop() > threshold);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
handleScroll(); // check inicial por si ya estamos scrolleados
|
|
100
|
+
target.addEventListener('scroll', handleScroll, { passive: true });
|
|
101
|
+
return () => target.removeEventListener('scroll', handleScroll);
|
|
102
|
+
}, [threshold, scrollTarget]);
|
|
103
|
+
|
|
104
|
+
const handleClick = () => {
|
|
105
|
+
scrollToTop({
|
|
106
|
+
target: scrollTarget ?? (typeof window !== 'undefined' ? window : undefined),
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const presetSx = resolvePreset('ScrollTopButton', preset, theme);
|
|
111
|
+
|
|
112
|
+
const mergedSx: SxProps<Theme> = [
|
|
113
|
+
positionSx[position],
|
|
114
|
+
{ zIndex: theme.zIndex.speedDial },
|
|
115
|
+
...(presetSx ? [presetSx] : []),
|
|
116
|
+
...(Array.isArray(sx) ? sx : sx ? [sx] : []),
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<Zoom in={visible} unmountOnExit>
|
|
121
|
+
<Fab
|
|
122
|
+
size={size}
|
|
123
|
+
color={color}
|
|
124
|
+
aria-label={ariaLabel}
|
|
125
|
+
onClick={handleClick}
|
|
126
|
+
className={className}
|
|
127
|
+
sx={mergedSx}
|
|
128
|
+
>
|
|
129
|
+
{icon ?? <KeyboardArrowUpIcon />}
|
|
130
|
+
</Fab>
|
|
131
|
+
</Zoom>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export default ScrollTopButton;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ScrollTopButton, default } from './ScrollTopButton';
|
|
2
|
+
export type {
|
|
3
|
+
ScrollTopButtonProps,
|
|
4
|
+
ScrollTopButtonPosition,
|
|
5
|
+
} from './ScrollTopButton';
|
|
6
|
+
|
|
7
|
+
export { scrollToTop } from './scrollToTop';
|
|
8
|
+
export type { ScrollToTopOptions } from './scrollToTop';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface ScrollToTopOptions {
|
|
2
|
+
/** Comportamiento del scroll. Default: `'smooth'`. */
|
|
3
|
+
behavior?: ScrollBehavior;
|
|
4
|
+
/**
|
|
5
|
+
* Elemento target del scroll. Default: `window`. Útil cuando el contenedor
|
|
6
|
+
* scrolleable NO es `window` (p.ej. un `<Box>` con `overflow: auto`).
|
|
7
|
+
*/
|
|
8
|
+
target?: Window | HTMLElement;
|
|
9
|
+
/** Offset desde el top. Default: `0`. */
|
|
10
|
+
top?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reemplaza imperativamente a `ScrollTopComponent.goTop()` de Metronic.
|
|
15
|
+
*
|
|
16
|
+
* Llamar tras mutaciones / cambios de filtros / apertura de modales para
|
|
17
|
+
* asegurar que el usuario vea el mensaje de confirmación en la parte
|
|
18
|
+
* superior de la página.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* // Reemplazo directo del legacy:
|
|
22
|
+
* // Antes: ScrollTopComponent.goTop()
|
|
23
|
+
* scrollToTop();
|
|
24
|
+
*
|
|
25
|
+
* // Con opciones:
|
|
26
|
+
* scrollToTop({ behavior: 'auto' });
|
|
27
|
+
* scrollToTop({ target: myScrollableRef.current });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function scrollToTop({
|
|
31
|
+
behavior = 'smooth',
|
|
32
|
+
target = typeof window !== 'undefined' ? window : undefined,
|
|
33
|
+
top = 0,
|
|
34
|
+
}: ScrollToTopOptions = {}): void {
|
|
35
|
+
if (!target) return;
|
|
36
|
+
target.scrollTo({ top, left: 0, behavior });
|
|
37
|
+
}
|