@companix/uikit 0.0.1
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/.eslintrc +54 -0
- package/declaration.d.ts +4 -0
- package/index.html +12 -0
- package/package.json +66 -0
- package/playground/App.tsx +166 -0
- package/playground/Example.tsx +14 -0
- package/playground/Test.tsx +44 -0
- package/playground/animation-test-1/index.scss +20 -0
- package/playground/animation-test-1/index.tsx +17 -0
- package/playground/animation-test-2/index.scss +62 -0
- package/playground/animation-test-2/index.tsx +32 -0
- package/playground/bootstrap.tsx +19 -0
- package/playground/buttons/index.tsx +132 -0
- package/playground/checkbox/index.tsx +64 -0
- package/playground/date-input/index.tsx +45 -0
- package/playground/date-picker/index.tsx +41 -0
- package/playground/dialog/index.tsx +92 -0
- package/playground/dialog-alert/index.tsx +47 -0
- package/playground/drawer/index.tsx +55 -0
- package/playground/index.css +33 -0
- package/playground/index.scss +270 -0
- package/playground/input/index.tsx +112 -0
- package/playground/number-inputs/index.tsx +50 -0
- package/playground/popovers/index.tsx +70 -0
- package/playground/radio-group/index.tsx +69 -0
- package/playground/select/index.tsx +72 -0
- package/playground/select-tags/index.tsx +36 -0
- package/playground/styles.scss +2 -0
- package/playground/switch/index.tsx +44 -0
- package/playground/tabs/index.tsx +16 -0
- package/playground/test.scss +0 -0
- package/playground/text-area/index.tsx +17 -0
- package/playground/text-input/index.tsx +12 -0
- package/playground/toaster/index.tsx +156 -0
- package/playground/tooltip/index.tsx +26 -0
- package/src/Button/Button.scss +128 -0
- package/src/Button/index.tsx +72 -0
- package/src/ButtonGroup/ButtonGroup.scss +18 -0
- package/src/ButtonGroup/index.tsx +20 -0
- package/src/Checkbox/Checkbox.scss +115 -0
- package/src/Checkbox/index.tsx +46 -0
- package/src/Countdown/index.tsx +54 -0
- package/src/DateInput/DateInput.scss +11 -0
- package/src/DateInput/index.tsx +96 -0
- package/src/DatePicker/Calendar.scss +125 -0
- package/src/DatePicker/Calendar.tsx +157 -0
- package/src/DatePicker/CalendarHeader.tsx +139 -0
- package/src/DatePicker/DatePicker.scss +0 -0
- package/src/DatePicker/index.tsx +177 -0
- package/src/Dialog/Dialog.scss +25 -0
- package/src/Dialog/Popup.scss +55 -0
- package/src/Dialog/index.tsx +31 -0
- package/src/DialogAlert/Alert.scss +52 -0
- package/src/DialogAlert/Alert.tsx +78 -0
- package/src/DialogAlert/Viewport.tsx +52 -0
- package/src/DialogAlert/index.tsx +37 -0
- package/src/Drawer/Drawer.scss +112 -0
- package/src/Drawer/index.tsx +46 -0
- package/src/File/index.tsx +60 -0
- package/src/Form/Form.scss +70 -0
- package/src/Form/Input.scss +24 -0
- package/src/Form/index.tsx +131 -0
- package/src/Icon/icon.scss +18 -0
- package/src/Icon/index.tsx +43 -0
- package/src/LoadButton/index.tsx +17 -0
- package/src/NumberInput/index.tsx +74 -0
- package/src/OptionItem/Option.scss +89 -0
- package/src/OptionItem/OptionItem.tsx +49 -0
- package/src/OptionItem/OptionsList.tsx +26 -0
- package/src/Popover/Popover.scss +80 -0
- package/src/Popover/index.tsx +117 -0
- package/src/Radio/Radio.scss +148 -0
- package/src/Radio/index.tsx +68 -0
- package/src/Scrollable/ImitateScroll.tsx +141 -0
- package/src/Scrollable/Scrollable.scss +50 -0
- package/src/Scrollable/index.tsx +141 -0
- package/src/Select/Select.scss +80 -0
- package/src/Select/SelectInput.tsx +131 -0
- package/src/Select/index.tsx +134 -0
- package/src/SelectTags/SelectTags.scss +66 -0
- package/src/SelectTags/index.tsx +192 -0
- package/src/Spinner/Spinner.scss +14 -0
- package/src/Spinner/index.tsx +19 -0
- package/src/Stepper/StepperInput.scss +35 -0
- package/src/Stepper/index.tsx +76 -0
- package/src/Switch/Switch.scss +102 -0
- package/src/Switch/index.tsx +49 -0
- package/src/Tabs/Tabs.scss +58 -0
- package/src/Tabs/index.tsx +89 -0
- package/src/TextArea/TextArea.scss +34 -0
- package/src/TextArea/index.tsx +51 -0
- package/src/Toaster/RemoveListener.tsx +11 -0
- package/src/Toaster/Toast.tsx +69 -0
- package/src/Toaster/Toaster.scss +151 -0
- package/src/Toaster/Viewport.tsx +117 -0
- package/src/Toaster/index.tsx +52 -0
- package/src/Tooltip/Tooltip.scss +28 -0
- package/src/Tooltip/index.tsx +33 -0
- package/src/__hooks/use-frooze-closing.ts +51 -0
- package/src/__hooks/use-loading.ts +34 -0
- package/src/__hooks/use-local-storage.ts +19 -0
- package/src/__hooks/use-popover-position.ts +24 -0
- package/src/__hooks/use-previos.ts +25 -0
- package/src/__hooks/use-resize.ts +41 -0
- package/src/__hooks/use-scrollbox.ts +45 -0
- package/src/__hooks/use-stepper-input.ts +82 -0
- package/src/__hooks/use-update.ts +19 -0
- package/src/__hooks/useCalendar.ts +104 -0
- package/src/__hooks/useCalendarOptions-copy.ts +87 -0
- package/src/__hooks/useCalendarOptions.ts +68 -0
- package/src/__libs/calendar.ts +175 -0
- package/src/__utils/utils.ts +137 -0
- package/src/css.scss +120 -0
- package/src/index.scss +22 -0
- package/src/index.ts +36 -0
- package/src/mixins.scss +99 -0
- package/src/theme.scss +103 -0
- package/src/types.ts +14 -0
- package/tailwind.config.js +91 -0
- package/themes/classic/animations.scss +179 -0
- package/themes/classic/classic.scss +493 -0
- package/tsconfig.json +27 -0
- package/vite.build.ts +35 -0
- package/vite.config.ts +33 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
@use '../mixins.scss';
|
|
2
|
+
|
|
3
|
+
.tabs {
|
|
4
|
+
display: flex;
|
|
5
|
+
align-items: flex-end;
|
|
6
|
+
flex: 0 0 auto;
|
|
7
|
+
position: relative;
|
|
8
|
+
|
|
9
|
+
@include mixins.use-styles(tab, container);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.tab-indicator-container {
|
|
13
|
+
left: 0;
|
|
14
|
+
pointer-events: none;
|
|
15
|
+
position: absolute;
|
|
16
|
+
top: 0;
|
|
17
|
+
|
|
18
|
+
@include mixins.use-styles(tab, indicator, container);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.tab-indicator {
|
|
22
|
+
position: absolute;
|
|
23
|
+
bottom: 0;
|
|
24
|
+
left: 0;
|
|
25
|
+
right: 0;
|
|
26
|
+
|
|
27
|
+
@include mixins.use-styles(tab, indicator);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.tab {
|
|
31
|
+
outline: none;
|
|
32
|
+
font-family: inherit;
|
|
33
|
+
flex: 1;
|
|
34
|
+
display: flex;
|
|
35
|
+
align-items: center;
|
|
36
|
+
justify-content: center;
|
|
37
|
+
line-height: 1;
|
|
38
|
+
background-color: transparent;
|
|
39
|
+
user-select: none;
|
|
40
|
+
position: relative;
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
font-family: inherit;
|
|
43
|
+
|
|
44
|
+
@include mixins.use-styles(tab);
|
|
45
|
+
|
|
46
|
+
&[data-disabled] {
|
|
47
|
+
cursor: not-allowed;
|
|
48
|
+
@include mixins.use-styles(tab, disabled);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
&:not([data-disabled]):not([data-state='active']):hover {
|
|
52
|
+
@include mixins.use-styles(tab, hover);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
&[data-state='active'] {
|
|
56
|
+
@include mixins.use-styles(tab, active);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as RadixTabs from '@radix-ui/react-tabs'
|
|
2
|
+
import { createContext, useContext, useEffect, useId, useRef, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
children: React.ReactNode
|
|
6
|
+
onChange: (value: string) => void
|
|
7
|
+
value: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const TabsContext = createContext({ baseId: '', listRef: {} as React.RefObject<HTMLDivElement> })
|
|
11
|
+
|
|
12
|
+
const makeTriggerId = (baseId: string, value: string) => {
|
|
13
|
+
return `radix-${baseId}-trigger-${value}`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const Tabs = ({ children, value, onChange }: Props) => {
|
|
17
|
+
const listRef = useRef<HTMLDivElement>(null)
|
|
18
|
+
const baseId = useId()
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<RadixTabs.Root value={value} onValueChange={onChange}>
|
|
22
|
+
<TabsContext.Provider value={{ baseId, listRef }}>
|
|
23
|
+
<RadixTabs.List className="tabs" ref={listRef}>
|
|
24
|
+
<TabIndicator value={value} />
|
|
25
|
+
{children}
|
|
26
|
+
</RadixTabs.List>
|
|
27
|
+
</TabsContext.Provider>
|
|
28
|
+
</RadixTabs.Root>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const TabIndicator = ({ value }: { value: string }) => {
|
|
33
|
+
const [styles, setStyles] = useState<React.CSSProperties>({})
|
|
34
|
+
const { baseId, listRef } = useContext(TabsContext)
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const state = { observer: null as null | ResizeObserver }
|
|
38
|
+
|
|
39
|
+
if (listRef.current) {
|
|
40
|
+
const tabIdSelector = `.tab[id="${makeTriggerId(baseId, value)}"]`
|
|
41
|
+
const selectedTabElement = listRef.current.querySelector<HTMLElement>(tabIdSelector)
|
|
42
|
+
|
|
43
|
+
if (selectedTabElement != null) {
|
|
44
|
+
state.observer = new ResizeObserver(() => {
|
|
45
|
+
const { clientHeight, clientWidth, offsetLeft, offsetTop } = selectedTabElement
|
|
46
|
+
|
|
47
|
+
setStyles({
|
|
48
|
+
height: clientHeight,
|
|
49
|
+
transform: `translateX(${Math.floor(offsetLeft)}px) translateY(${Math.floor(offsetTop)}px)`,
|
|
50
|
+
width: clientWidth
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
state.observer.observe(selectedTabElement)
|
|
55
|
+
} else {
|
|
56
|
+
setStyles({ display: 'none' })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return () => {
|
|
61
|
+
if (state.observer) {
|
|
62
|
+
state.observer.disconnect()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}, [value])
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<div className="tab-indicator-container" style={styles}>
|
|
69
|
+
<div className="tab-indicator" />
|
|
70
|
+
</div>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface TabProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
75
|
+
value: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const Tab = ({ children, value, ...restProps }: TabProps) => {
|
|
79
|
+
const { baseId } = useContext(TabsContext)
|
|
80
|
+
const id = makeTriggerId(baseId, value)
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<RadixTabs.Trigger {...restProps} id={id} className="tab" value={value}>
|
|
84
|
+
{children}
|
|
85
|
+
</RadixTabs.Trigger>
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Tabs.Tab = Tab
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
.form-textarea {
|
|
2
|
+
display: flex;
|
|
3
|
+
|
|
4
|
+
textarea {
|
|
5
|
+
padding: var(--form_space-y, 0px) var(--form_space, 0px);
|
|
6
|
+
border-radius: var(--form_border-radius);
|
|
7
|
+
border-style: solid;
|
|
8
|
+
border-width: 2px;
|
|
9
|
+
border-top-width: 0;
|
|
10
|
+
border-left-width: 0;
|
|
11
|
+
border-color: transparent;
|
|
12
|
+
min-height: max-content;
|
|
13
|
+
resize: vertical;
|
|
14
|
+
display: block;
|
|
15
|
+
|
|
16
|
+
::-webkit-search-cancel-button {
|
|
17
|
+
display: none;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
::-webkit-search-decoration {
|
|
21
|
+
-webkit-appearance: none;
|
|
22
|
+
appearance: none;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
::-ms-clear {
|
|
26
|
+
display: none;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
&[data-grow] {
|
|
30
|
+
resize: none;
|
|
31
|
+
border: none;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useResizeTextarea } from '../__hooks/use-resize'
|
|
2
|
+
import { attr, callMultiple } from '@companix/utils-browser'
|
|
3
|
+
import { useEffect } from 'react'
|
|
4
|
+
|
|
5
|
+
interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
|
6
|
+
/**
|
|
7
|
+
* Свойство управляющее автоматическим изменением высоты компонента.
|
|
8
|
+
*/
|
|
9
|
+
grow?: boolean
|
|
10
|
+
/**
|
|
11
|
+
* Обработчик, срабатывающий при изменении размера компонента.
|
|
12
|
+
*/
|
|
13
|
+
onResize?: (el: HTMLTextAreaElement) => void
|
|
14
|
+
fill?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const TextArea = ({
|
|
18
|
+
onResize,
|
|
19
|
+
grow = false,
|
|
20
|
+
value,
|
|
21
|
+
onChange,
|
|
22
|
+
disabled,
|
|
23
|
+
required,
|
|
24
|
+
rows = 2,
|
|
25
|
+
fill = false,
|
|
26
|
+
...textAreaProps
|
|
27
|
+
}: TextAreaProps) => {
|
|
28
|
+
const [refResizeTextarea, resize] = useResizeTextarea(onResize, grow)
|
|
29
|
+
|
|
30
|
+
useEffect(resize, [resize, value])
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<div
|
|
34
|
+
className="form form-textarea"
|
|
35
|
+
data-required={attr(required)}
|
|
36
|
+
data-disabled={attr(disabled)}
|
|
37
|
+
data-fill={attr(fill)}
|
|
38
|
+
>
|
|
39
|
+
<textarea
|
|
40
|
+
className="form-input"
|
|
41
|
+
data-grow={attr(grow)}
|
|
42
|
+
value={value}
|
|
43
|
+
ref={refResizeTextarea}
|
|
44
|
+
rows={rows}
|
|
45
|
+
disabled={disabled}
|
|
46
|
+
onChange={callMultiple(onChange, resize)}
|
|
47
|
+
{...textAreaProps}
|
|
48
|
+
/>
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as ToasterPrimitive from '@radix-ui/react-toast'
|
|
2
|
+
import { attr } from '@companix/utils-browser'
|
|
3
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
|
4
|
+
import { ToastOptions } from '.'
|
|
5
|
+
import { RemoveListener } from './RemoveListener'
|
|
6
|
+
|
|
7
|
+
interface ToastProps extends ToastOptions {
|
|
8
|
+
onRemoved: () => void
|
|
9
|
+
onRemoving: () => void
|
|
10
|
+
onInitialized: (ref: HTMLLIElement) => void
|
|
11
|
+
closeIcon?: React.ReactNode
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const Toast = (options: ToastProps) => {
|
|
15
|
+
const [mounted, setMounted] = useState(false)
|
|
16
|
+
|
|
17
|
+
const ref = useRef<HTMLLIElement>(null)
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
appearance = 'neutral',
|
|
21
|
+
icon,
|
|
22
|
+
title,
|
|
23
|
+
description,
|
|
24
|
+
duration,
|
|
25
|
+
closeIcon,
|
|
26
|
+
onRemoving,
|
|
27
|
+
onRemoved,
|
|
28
|
+
onInitialized
|
|
29
|
+
} = options
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
setMounted(true)
|
|
33
|
+
}, [])
|
|
34
|
+
|
|
35
|
+
useLayoutEffect(() => {
|
|
36
|
+
if (ref.current) {
|
|
37
|
+
onInitialized(ref.current)
|
|
38
|
+
}
|
|
39
|
+
}, [])
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<ToasterPrimitive.Root
|
|
43
|
+
ref={ref}
|
|
44
|
+
data-expanded
|
|
45
|
+
data-appearance={appearance}
|
|
46
|
+
data-mounted={attr(mounted)}
|
|
47
|
+
className="toaster"
|
|
48
|
+
duration={duration}
|
|
49
|
+
>
|
|
50
|
+
<RemoveListener callback={onRemoved} />
|
|
51
|
+
{icon && <div className="toaster-icon">{icon}</div>}
|
|
52
|
+
<div className="toaster-content">
|
|
53
|
+
{title && <ToasterPrimitive.Title className="toaster-title">{title}</ToasterPrimitive.Title>}
|
|
54
|
+
{description && (
|
|
55
|
+
<ToasterPrimitive.Description className="toaster-description">
|
|
56
|
+
{description}
|
|
57
|
+
</ToasterPrimitive.Description>
|
|
58
|
+
)}
|
|
59
|
+
</div>
|
|
60
|
+
<ToasterPrimitive.Close className="toaster-close" onClick={onRemoving}>
|
|
61
|
+
{closeIcon ?? (
|
|
62
|
+
<svg viewBox="0 0 16 16" fill="currentColor">
|
|
63
|
+
<path d="M2.96967 2.96967C3.26256 2.67678 3.73744 2.67678 4.03033 2.96967L8 6.939L11.9697 2.96967C12.2626 2.67678 12.7374 2.67678 13.0303 2.96967C13.3232 3.26256 13.3232 3.73744 13.0303 4.03033L9.061 8L13.0303 11.9697C13.2966 12.2359 13.3208 12.6526 13.1029 12.9462L13.0303 13.0303C12.7374 13.3232 12.2626 13.3232 11.9697 13.0303L8 9.061L4.03033 13.0303C3.73744 13.3232 3.26256 13.3232 2.96967 13.0303C2.67678 12.7374 2.67678 12.2626 2.96967 11.9697L6.939 8L2.96967 4.03033C2.7034 3.76406 2.6792 3.3474 2.89705 3.05379L2.96967 2.96967Z" />
|
|
64
|
+
</svg>
|
|
65
|
+
)}
|
|
66
|
+
</ToasterPrimitive.Close>
|
|
67
|
+
</ToasterPrimitive.Root>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
@use '../mixins.scss';
|
|
2
|
+
|
|
3
|
+
@mixin use-appearance($appearance) {
|
|
4
|
+
&[data-appearance='#{$appearance}'] {
|
|
5
|
+
@include mixins.use-styles(toaster, $appearance);
|
|
6
|
+
|
|
7
|
+
.toaster {
|
|
8
|
+
&-icon {
|
|
9
|
+
@include mixins.use-styles(toaster, $appearance, icon);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
&-title {
|
|
13
|
+
@include mixins.use-styles(toaster, $appearance, title);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
&-description {
|
|
17
|
+
@include mixins.use-styles(toaster, $appearance, description);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.toaster {
|
|
24
|
+
pointer-events: all;
|
|
25
|
+
position: absolute;
|
|
26
|
+
outline: none;
|
|
27
|
+
opacity: 1;
|
|
28
|
+
transition: transform 0.4s, opacity 0.4s, height 0.4s, box-shadow 0.2s;
|
|
29
|
+
transform: var(--y);
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
|
|
33
|
+
@include mixins.use-styles(toaster);
|
|
34
|
+
|
|
35
|
+
// appearance
|
|
36
|
+
|
|
37
|
+
@include use-appearance(primary);
|
|
38
|
+
@include use-appearance(neutral);
|
|
39
|
+
@include use-appearance(negative);
|
|
40
|
+
@include use-appearance(positive);
|
|
41
|
+
@include use-appearance(warning);
|
|
42
|
+
|
|
43
|
+
&[data-expanded]:not([data-mounted]) {
|
|
44
|
+
opacity: 0;
|
|
45
|
+
--y: translateY(calc(var(--lift) * -100% - var(--toaster_viewport_offset, 0)));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
&[data-expanded][data-mounted] {
|
|
49
|
+
--y: translateY(calc(var(--lift) * var(--offset)));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
&[data-state='closed'] {
|
|
53
|
+
animation: toastOut 400ms, toastFadeOut 400ms;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
&-viewport {
|
|
57
|
+
position: fixed;
|
|
58
|
+
display: flex;
|
|
59
|
+
flex-direction: column;
|
|
60
|
+
left: var(--toaster_viewport_offset);
|
|
61
|
+
right: var(--toaster_viewport_offset);
|
|
62
|
+
z-index: 40;
|
|
63
|
+
outline: none;
|
|
64
|
+
pointer-events: none;
|
|
65
|
+
gap: var(--toasters-gap, 0);
|
|
66
|
+
|
|
67
|
+
&[data-side='top'] {
|
|
68
|
+
top: var(--toaster_viewport_offset);
|
|
69
|
+
|
|
70
|
+
.toaster {
|
|
71
|
+
--lift: 1;
|
|
72
|
+
top: 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
&[data-side='bottom'] {
|
|
77
|
+
bottom: var(--toaster_viewport_offset);
|
|
78
|
+
top: auto;
|
|
79
|
+
|
|
80
|
+
.toaster {
|
|
81
|
+
--lift: -1;
|
|
82
|
+
bottom: 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
&[data-align='center'] {
|
|
87
|
+
align-items: center;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
&[data-align='right'] {
|
|
91
|
+
align-items: flex-end;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
&[data-align='left'] {
|
|
95
|
+
align-items: flex-start;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
&-content {
|
|
100
|
+
display: flex;
|
|
101
|
+
flex-direction: column;
|
|
102
|
+
|
|
103
|
+
@include mixins.use-styles(toaster, content);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
&-title {
|
|
107
|
+
@include mixins.use-styles(toaster, title);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
&-description {
|
|
111
|
+
@include mixins.use-styles(toaster, description);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
&-close {
|
|
115
|
+
cursor: pointer;
|
|
116
|
+
top: var(--toaster_close_space, 0);
|
|
117
|
+
right: var(--toaster_close_space, 0);
|
|
118
|
+
position: absolute;
|
|
119
|
+
transition: color 0.2s;
|
|
120
|
+
|
|
121
|
+
@include mixins.use-styles(toaster, close);
|
|
122
|
+
|
|
123
|
+
&:hover {
|
|
124
|
+
@include mixins.use-styles(toaster, close, hover);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
svg {
|
|
128
|
+
@include mixins.use-size(toaster, close, size);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
&-icon {
|
|
133
|
+
@include mixins.use-styles(toaster, icon);
|
|
134
|
+
|
|
135
|
+
svg {
|
|
136
|
+
@include mixins.use-size(toaster, icon, size);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
@keyframes toastOut {
|
|
142
|
+
to {
|
|
143
|
+
transform: translateY(calc(var(--lift) * -100% - var(--toaster_viewport_offset)));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@keyframes toastFadeOut {
|
|
148
|
+
to {
|
|
149
|
+
opacity: 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as ToasterPrimitive from '@radix-ui/react-toast'
|
|
2
|
+
import { varToStyle } from '@companix/utils-browser'
|
|
3
|
+
import { useCallback, useMemo, useState, useImperativeHandle, forwardRef } from 'react'
|
|
4
|
+
import { Toast } from './Toast'
|
|
5
|
+
import { InnerToast } from '.'
|
|
6
|
+
|
|
7
|
+
enum SwipesDirections {
|
|
8
|
+
'top-center' = 'up',
|
|
9
|
+
'top-left' = 'left',
|
|
10
|
+
'top-right' = 'right',
|
|
11
|
+
'bottom-center' = 'down',
|
|
12
|
+
'bottom-left' = 'left',
|
|
13
|
+
'bottom-right' = 'right'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ViewportProps {
|
|
17
|
+
side?: 'top' | 'bottom'
|
|
18
|
+
align?: 'center' | 'left' | 'right'
|
|
19
|
+
gap?: number // gap between toasts
|
|
20
|
+
swipeThreshold?: number
|
|
21
|
+
duration?: number
|
|
22
|
+
closeIcon?: React.ReactNode
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ViewportRef {
|
|
26
|
+
addToast: (toast: InnerToast) => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const Viewport = forwardRef<ViewportRef, ViewportProps>((props, ref) => {
|
|
30
|
+
const { side = 'top', align = 'center', gap = 14, duration, swipeThreshold, closeIcon } = props
|
|
31
|
+
const [toasters, setToasters] = useState<InnerToast[]>([])
|
|
32
|
+
|
|
33
|
+
const refs = useMemo((): { [id: string]: HTMLLIElement } => {
|
|
34
|
+
return {}
|
|
35
|
+
}, [])
|
|
36
|
+
|
|
37
|
+
useImperativeHandle(
|
|
38
|
+
ref,
|
|
39
|
+
() => {
|
|
40
|
+
return {
|
|
41
|
+
addToast: (toast) => {
|
|
42
|
+
setToasters((state) => [...state, toast])
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
[]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const applyOffsets = useCallback((toasters: InnerToast[]) => {
|
|
50
|
+
toasters.forEach(({ id }, index) => {
|
|
51
|
+
let offset = 0
|
|
52
|
+
|
|
53
|
+
for (let i = index + 1; i < toasters.length; i++) {
|
|
54
|
+
if (refs[toasters[i].id]) {
|
|
55
|
+
offset += refs[toasters[i].id].clientHeight + gap
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (refs[id]) {
|
|
60
|
+
refs[id].style.setProperty('--offset', `${offset}px`)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
}, [])
|
|
64
|
+
|
|
65
|
+
const handleClose = (id: string) => {
|
|
66
|
+
setToasters((state) => {
|
|
67
|
+
const nextState = [...state]
|
|
68
|
+
const index = nextState.findIndex((item) => item.id === id)
|
|
69
|
+
|
|
70
|
+
if (index !== -1) {
|
|
71
|
+
nextState.splice(index, 1)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return nextState
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<ToasterPrimitive.Provider
|
|
80
|
+
duration={duration}
|
|
81
|
+
swipeThreshold={swipeThreshold}
|
|
82
|
+
swipeDirection={SwipesDirections[`${side}-${align}`]}
|
|
83
|
+
>
|
|
84
|
+
{toasters.map(({ id, ...toast }) => (
|
|
85
|
+
<Toast
|
|
86
|
+
{...toast}
|
|
87
|
+
key={`toaster-${id}`}
|
|
88
|
+
closeIcon={closeIcon}
|
|
89
|
+
onInitialized={(ref) => {
|
|
90
|
+
refs[id] = ref
|
|
91
|
+
applyOffsets(toasters)
|
|
92
|
+
}}
|
|
93
|
+
onRemoving={() => {
|
|
94
|
+
delete refs[id]
|
|
95
|
+
applyOffsets(toasters)
|
|
96
|
+
}}
|
|
97
|
+
onRemoved={() => {
|
|
98
|
+
if (refs[id]) {
|
|
99
|
+
// Если коллбек onRemoved вызвается при истечении duration,
|
|
100
|
+
// мы не перехватываем событие onRemoving и не изменяем позиции текущих тостов
|
|
101
|
+
// (автоматическое закрытие тостов всегда происходит в порядке открытия)
|
|
102
|
+
delete refs[id]
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
handleClose(id)
|
|
106
|
+
}}
|
|
107
|
+
/>
|
|
108
|
+
))}
|
|
109
|
+
<ToasterPrimitive.Viewport
|
|
110
|
+
data-side={side}
|
|
111
|
+
data-align={align}
|
|
112
|
+
className="toaster-viewport"
|
|
113
|
+
style={varToStyle({ '--toasters-gap': `${gap}px` })}
|
|
114
|
+
/>
|
|
115
|
+
</ToasterPrimitive.Provider>
|
|
116
|
+
)
|
|
117
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { hash } from '@companix/utils-js'
|
|
2
|
+
import { Viewport, ViewportProps, ViewportRef } from './Viewport'
|
|
3
|
+
import { useMemo, useRef } from 'react'
|
|
4
|
+
|
|
5
|
+
export interface ToastOptions {
|
|
6
|
+
appearance?: 'primary' | 'neutral' | 'positive' | 'negative' | 'warning'
|
|
7
|
+
icon?: React.ReactNode
|
|
8
|
+
title?: React.ReactNode
|
|
9
|
+
description?: React.ReactNode
|
|
10
|
+
duration?: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface InnerToast extends ToastOptions {
|
|
14
|
+
id: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const createToaster = (rootProps: ViewportProps = {}) => {
|
|
18
|
+
const store = {
|
|
19
|
+
emit: (toast: InnerToast) => {
|
|
20
|
+
console.error('uninitialized', toast)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
send: (toast: ToastOptions) => {
|
|
26
|
+
store.emit({ ...toast, id: hash() })
|
|
27
|
+
},
|
|
28
|
+
Viewport: (props: ViewportProps = {}) => {
|
|
29
|
+
const ref = useRef<ViewportRef>(null)
|
|
30
|
+
|
|
31
|
+
useMemo(() => {
|
|
32
|
+
store.emit = (value) => {
|
|
33
|
+
if (ref.current) {
|
|
34
|
+
ref.current.addToast(value)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}, [])
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<Viewport
|
|
41
|
+
ref={ref}
|
|
42
|
+
align={props.align ?? rootProps.align}
|
|
43
|
+
closeIcon={props.closeIcon ?? rootProps.closeIcon}
|
|
44
|
+
duration={props.duration ?? rootProps.duration}
|
|
45
|
+
gap={props.gap ?? rootProps.gap}
|
|
46
|
+
side={props.side ?? rootProps.side}
|
|
47
|
+
swipeThreshold={props.swipeThreshold ?? rootProps.swipeThreshold}
|
|
48
|
+
/>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
@use '../mixins.scss';
|
|
2
|
+
|
|
3
|
+
.tooltip .popover {
|
|
4
|
+
&-content {
|
|
5
|
+
@include mixins.use-styles(tooltip);
|
|
6
|
+
max-height: none;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
&-arrow {
|
|
10
|
+
&-fill {
|
|
11
|
+
fill: mixins.get-var-name(tooltip, background);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.tooltip {
|
|
17
|
+
&[data-appearance='default'] {
|
|
18
|
+
&[data-state='open'] {
|
|
19
|
+
transform-origin: var(--radix-popover-content-transform-origin);
|
|
20
|
+
@include mixins.use-styles(tooltip, in);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
&[data-state='closed'] {
|
|
24
|
+
transform-origin: var(--radix-popover-content-transform-origin);
|
|
25
|
+
@include mixins.use-styles(tooltip, out);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import { Popover } from '..'
|
|
3
|
+
import { Side } from '../Popover'
|
|
4
|
+
|
|
5
|
+
interface TooltipProps {
|
|
6
|
+
children: React.ReactNode
|
|
7
|
+
content: React.ReactNode
|
|
8
|
+
side?: Side
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const Tooltip = ({ children, content, side }: TooltipProps) => {
|
|
12
|
+
const [open, setOpen] = useState(false)
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<Popover
|
|
16
|
+
showArrows
|
|
17
|
+
className="tooltip"
|
|
18
|
+
open={open}
|
|
19
|
+
side={side}
|
|
20
|
+
content={() => content}
|
|
21
|
+
triggerProps={{
|
|
22
|
+
onMouseEnter: () => {
|
|
23
|
+
setOpen(true)
|
|
24
|
+
},
|
|
25
|
+
onMouseLeave: () => {
|
|
26
|
+
setOpen(false)
|
|
27
|
+
}
|
|
28
|
+
}}
|
|
29
|
+
>
|
|
30
|
+
{children}
|
|
31
|
+
</Popover>
|
|
32
|
+
)
|
|
33
|
+
}
|