@vitessce/styles 3.9.1 → 3.9.3

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/src/Popper.tsx ADDED
@@ -0,0 +1,249 @@
1
+ /*
2
+ * This is the Popper component from MUI v4 which uses Popper.js v1.
3
+ * The Popper in MUI v7 uses Popper.js v2 and does not seem to respect the anchorEl's positioning,
4
+ * which is the reason we copy the v4 version here.
5
+ * Reference: https://github.com/mui/material-ui/blob/v4.x/packages/material-ui/src/Popper/Popper.js
6
+ */
7
+ import * as React from 'react';
8
+ import PopperJs, { Placement, Modifiers, Data } from 'popper.js';
9
+ import { useStyles as useTheme } from 'tss-react/mui';
10
+ import { Portal } from '@mui/material';
11
+ import { createChainedFunction, setRef, useForkRef } from './utils.js';
12
+
13
+ interface Theme {
14
+ direction?: 'ltr' | 'rtl';
15
+ }
16
+
17
+ function flipPlacement(placement: Placement, theme?: Theme): Placement {
18
+ const direction = (theme && theme.direction) || 'ltr';
19
+
20
+ if (direction === 'ltr') {
21
+ return placement;
22
+ }
23
+
24
+ switch (placement) {
25
+ case 'bottom-end':
26
+ return 'bottom-start';
27
+ case 'bottom-start':
28
+ return 'bottom-end';
29
+ case 'top-end':
30
+ return 'top-start';
31
+ case 'top-start':
32
+ return 'top-end';
33
+ default:
34
+ return placement;
35
+ }
36
+ }
37
+
38
+ type AnchorElType = null | Element | ((element: Element) => Element);
39
+
40
+ function getAnchorEl(anchorEl: AnchorElType): Element | null {
41
+ return typeof anchorEl === 'function' ? anchorEl(document.body) : anchorEl;
42
+ }
43
+
44
+ const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
45
+
46
+ interface PopperOptions {
47
+ placement?: Placement;
48
+ modifiers?: Modifiers;
49
+ onCreate?: (data: Data) => void;
50
+ onUpdate?: (data: Data) => void;
51
+ }
52
+
53
+ const defaultPopperOptions: PopperOptions = {};
54
+
55
+ interface TransitionProps {
56
+ in?: boolean;
57
+ onEnter?: () => void;
58
+ onExited?: () => void;
59
+ }
60
+
61
+ interface ChildProps {
62
+ placement: Placement;
63
+ TransitionProps?: TransitionProps;
64
+ }
65
+
66
+ export interface PopperProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
67
+ anchorEl?: AnchorElType;
68
+ children: React.ReactNode | ((props: ChildProps) => React.ReactNode);
69
+ container?: Element | (() => Element | null) | null;
70
+ disablePortal?: boolean;
71
+ keepMounted?: boolean;
72
+ modifiers?: Modifiers;
73
+ open: boolean;
74
+ placement?: Placement;
75
+ popperOptions?: PopperOptions;
76
+ popperRef?: React.Ref<PopperJs | null>;
77
+ style?: React.CSSProperties;
78
+ transition?: boolean;
79
+ }
80
+
81
+ /**
82
+ * Poppers rely on the 3rd party library [Popper.js](https://popper.js.org/docs/v1/) for positioning.
83
+ */
84
+ export const Popper = React.forwardRef<HTMLDivElement, PopperProps>(
85
+ (props, ref) => {
86
+ const {
87
+ anchorEl,
88
+ children,
89
+ container,
90
+ disablePortal = false,
91
+ keepMounted = false,
92
+ modifiers,
93
+ open,
94
+ placement: initialPlacement = 'bottom',
95
+ popperOptions = defaultPopperOptions,
96
+ popperRef: popperRefProp,
97
+ style,
98
+ transition = false,
99
+ ...other
100
+ } = props;
101
+ const tooltipRef = React.useRef<HTMLDivElement>(null);
102
+ const ownRef = useForkRef(tooltipRef, ref);
103
+
104
+ const popperRef = React.useRef<PopperJs | null>(null);
105
+ const handlePopperRef = useForkRef(popperRef, popperRefProp);
106
+ const handlePopperRefRef = React.useRef(handlePopperRef);
107
+ useEnhancedEffect(() => {
108
+ handlePopperRefRef.current = handlePopperRef;
109
+ }, [handlePopperRef]);
110
+ React.useImperativeHandle(popperRefProp, () => popperRef.current, []);
111
+
112
+ const [exited, setExited] = React.useState(true);
113
+
114
+ const { theme } = useTheme();
115
+ const rtlPlacement = flipPlacement(initialPlacement, theme);
116
+ /**
117
+ * placement initialized from prop but can change during lifetime if modifiers.flip.
118
+ * modifiers.flip is essentially a flip for controlled/uncontrolled behavior
119
+ */
120
+ const [placement, setPlacement] = React.useState(rtlPlacement);
121
+
122
+ React.useEffect(() => {
123
+ if (popperRef.current) {
124
+ popperRef.current.update();
125
+ }
126
+ });
127
+
128
+ const handleOpen = React.useCallback(() => {
129
+ if (!tooltipRef.current || !anchorEl || !open) {
130
+ return;
131
+ }
132
+
133
+ if (popperRef.current) {
134
+ popperRef.current.destroy();
135
+ if (handlePopperRefRef.current) {
136
+ setRef(handlePopperRefRef.current, null);
137
+ }
138
+ }
139
+
140
+ const handlePopperUpdate = (data: Data) => {
141
+ setPlacement(data.placement);
142
+ };
143
+
144
+ const anchorElement = getAnchorEl(anchorEl);
145
+ if (!anchorElement) {
146
+ return;
147
+ }
148
+
149
+ const popper = new PopperJs(anchorElement, tooltipRef.current, {
150
+ placement: rtlPlacement,
151
+ ...popperOptions,
152
+ modifiers: {
153
+ ...(disablePortal
154
+ ? {}
155
+ : {
156
+ // It's using scrollParent by default, we can use the viewport when using a portal.
157
+ preventOverflow: {
158
+ boundariesElement: 'window',
159
+ },
160
+ }),
161
+ ...modifiers,
162
+ ...popperOptions.modifiers,
163
+ },
164
+ // We could have been using a custom modifier like react-popper is doing.
165
+ // But it seems this is the best public API for this use case.
166
+ onCreate: createChainedFunction(handlePopperUpdate, popperOptions.onCreate),
167
+ onUpdate: createChainedFunction(handlePopperUpdate, popperOptions.onUpdate),
168
+ });
169
+
170
+ if (handlePopperRefRef.current) {
171
+ setRef(handlePopperRefRef.current, popper);
172
+ }
173
+ }, [anchorEl, disablePortal, modifiers, open, rtlPlacement, popperOptions]);
174
+
175
+ const handleRef = React.useCallback(
176
+ (node: HTMLDivElement | null) => {
177
+ setRef(ownRef, node);
178
+ handleOpen();
179
+ },
180
+ [ownRef, handleOpen],
181
+ );
182
+
183
+ const handleEnter = () => {
184
+ setExited(false);
185
+ };
186
+
187
+ const handleClose = () => {
188
+ if (!popperRef.current) {
189
+ return;
190
+ }
191
+
192
+ popperRef.current.destroy();
193
+ if (handlePopperRefRef.current) {
194
+ setRef(handlePopperRefRef.current, null);
195
+ }
196
+ };
197
+
198
+ const handleExited = () => {
199
+ setExited(true);
200
+ handleClose();
201
+ };
202
+
203
+ React.useEffect(() => () => {
204
+ handleClose();
205
+ }, []);
206
+
207
+ React.useEffect(() => {
208
+ if (!open && !transition) {
209
+ // Otherwise handleExited will call this.
210
+ handleClose();
211
+ }
212
+ }, [open, transition]);
213
+
214
+ if (!keepMounted && !open && (!transition || exited)) {
215
+ return null;
216
+ }
217
+
218
+ const childProps: ChildProps = { placement };
219
+
220
+ if (transition) {
221
+ childProps.TransitionProps = {
222
+ in: open,
223
+ onEnter: handleEnter,
224
+ onExited: handleExited,
225
+ };
226
+ }
227
+
228
+ return (
229
+ <Portal disablePortal={disablePortal} container={container}>
230
+ <div
231
+ ref={handleRef}
232
+ role="tooltip"
233
+ {...other}
234
+ style={{
235
+ // Prevents scroll issue, waiting for Popper.js to add this style once initiated.
236
+ position: 'fixed',
237
+ // Fix Popper.js display issue
238
+ top: 0,
239
+ left: 0,
240
+ display: !open && keepMounted && !transition ? 'none' : undefined,
241
+ ...style,
242
+ }}
243
+ >
244
+ {typeof children === 'function' ? children(childProps) : children}
245
+ </div>
246
+ </Portal>
247
+ );
248
+ },
249
+ );
@@ -0,0 +1,30 @@
1
+ import React, { useMemo } from 'react';
2
+ import { GlobalStyles, CSSObject } from 'tss-react';
3
+
4
+
5
+ function prependParentClassName(jssObject: CSSObject, parentClassName: string): CSSObject {
6
+ return Object.entries(jssObject).reduce<CSSObject>((accumulator, [key, value]) => {
7
+ accumulator[`.${parentClassName} ${key}`] = value;
8
+ return accumulator;
9
+ }, {});
10
+ }
11
+
12
+ export interface ScopedGlobalStylesProps {
13
+ styles: CSSObject;
14
+ parentClassName: string;
15
+ }
16
+
17
+ export function ScopedGlobalStyles(props: ScopedGlobalStylesProps) {
18
+ const { styles, parentClassName } = props;
19
+
20
+ const component = useMemo(() => {
21
+ const computedStyles = prependParentClassName(
22
+ styles,
23
+ parentClassName,
24
+ );
25
+ return (
26
+ <GlobalStyles styles={computedStyles} />
27
+ );
28
+ }, [parentClassName, styles]);
29
+ return component;
30
+ }
@@ -0,0 +1,172 @@
1
+ import React, { useRef, useEffect, useState, type ComponentType } from 'react';
2
+ import {
3
+ VariableSizeList as _VariableSizeList,
4
+ type VariableSizeList as VariableSizeListType,
5
+ } from 'react-window'; // TODO: upgrade to v2
6
+ import Autocomplete from '@mui/material/Autocomplete';
7
+ import TextField from '@mui/material/TextField';
8
+ import Typography from '@mui/material/Typography';
9
+ import { makeStyles } from 'tss-react/mui';
10
+
11
+ // Cast to work around react-window types being incompatible with React 18 types.
12
+ const VariableSizeList = _VariableSizeList as unknown as ComponentType<any>;
13
+
14
+ export interface AutocompleteItem {
15
+ label: string;
16
+ data: any; // Additional data can be included as needed
17
+ }
18
+
19
+ export interface SimpleAutocompleteProps {
20
+ /**
21
+ * Async function that takes a search string and returns matching items.
22
+ */
23
+ getMatches: (inputValue: string) => Promise<AutocompleteItem[]>;
24
+ /**
25
+ * Callback fired when the user selects an item from the dropdown.
26
+ */
27
+ onChange: (item: AutocompleteItem | null) => void;
28
+ /**
29
+ * Label for the text input.
30
+ */
31
+ textInputLabel?: string;
32
+ /**
33
+ * Function to get the label for an option, used in rendering the dropdown.
34
+ */
35
+ getItemLabel?: (option: AutocompleteItem) => string;
36
+ }
37
+
38
+ const LIST_ROW_HEIGHT = 48;
39
+
40
+ const useStyles = makeStyles()(() => ({
41
+ searchInput: {
42
+ lineHeight: 'initial',
43
+ height: 'auto !important',
44
+ },
45
+ }));
46
+
47
+ // Context to forward outer element props for the virtualized listbox.
48
+ const OuterElementContext = React.createContext({});
49
+
50
+ const OuterElementType = React.forwardRef<
51
+ HTMLDivElement,
52
+ React.HTMLAttributes<HTMLDivElement>
53
+ >((props, ref) => {
54
+ const outerProps = React.useContext(OuterElementContext);
55
+ return <div ref={ref} {...props} {...outerProps} />;
56
+ });
57
+
58
+ function ListRow(props: { data: any[]; index: number; style: React.CSSProperties }) {
59
+ const { data, index, style } = props;
60
+ const dataSet = data[index];
61
+ const inlineStyle = {
62
+ ...style,
63
+ top: (style.top as number) + 8,
64
+ };
65
+
66
+ // This array is created by the renderOption function in the Autocomplete component.
67
+ const [props0, option, getItemLabel] = dataSet;
68
+ const { key, ...optionProps } = props0;
69
+
70
+ return (
71
+ <Typography key={key} component="li" {...optionProps} noWrap style={inlineStyle}>
72
+ {getItemLabel(option)}
73
+ </Typography>
74
+ );
75
+ }
76
+
77
+ function useResetCache(itemCount: number) {
78
+ const ref = useRef<VariableSizeListType>(null);
79
+ useEffect(() => {
80
+ if (ref.current != null) {
81
+ ref.current.resetAfterIndex(0, true);
82
+ }
83
+ }, [itemCount]);
84
+ return ref;
85
+ }
86
+
87
+ // Virtualized listbox based on:
88
+ // https://mui.com/material-ui/react-autocomplete/#virtualization
89
+ const ListboxComponent = React.forwardRef<
90
+ HTMLDivElement,
91
+ React.HTMLAttributes<HTMLDivElement>
92
+ >((props, ref) => {
93
+ const { children, ...other } = props;
94
+
95
+ const items = children as any[];
96
+ const itemCount = items.length;
97
+ const gridRef = useResetCache(itemCount);
98
+
99
+ return (
100
+ <div ref={ref}>
101
+ <OuterElementContext.Provider value={other}>
102
+ <VariableSizeList
103
+ height={Math.min(8, itemCount) * LIST_ROW_HEIGHT}
104
+ itemSize={() => LIST_ROW_HEIGHT}
105
+ width="100%"
106
+ innerElementType="ul"
107
+ outerElementType={OuterElementType as any}
108
+ overscanCount={5}
109
+ ref={gridRef}
110
+ itemCount={itemCount}
111
+ itemData={items}
112
+ >
113
+ {ListRow}
114
+ </VariableSizeList>
115
+ </OuterElementContext.Provider>
116
+ </div>
117
+ );
118
+ });
119
+
120
+ /**
121
+ * A reusable virtualized autocomplete for searching genes (and other
122
+ * ontology nodes such as proteins, pathways, or cell types).
123
+ *
124
+ * The caller supplies an async `getMatches` function that receives
125
+ * the current input value and returns matching `AutocompleteItem[]`.
126
+ */
127
+ export function SimpleAutocomplete(props: SimpleAutocompleteProps) {
128
+ const {
129
+ getMatches,
130
+ onChange,
131
+ textInputLabel = 'Enter a gene',
132
+ getItemLabel = (option: AutocompleteItem) => option.label,
133
+ } = props;
134
+ const { classes } = useStyles();
135
+
136
+ const [potentialItems, setPotentialItems] = useState<AutocompleteItem[]>([]);
137
+
138
+ const handleInputChange = async (
139
+ _event: React.SyntheticEvent,
140
+ value: string,
141
+ ) => {
142
+ if (!value) {
143
+ setPotentialItems([]);
144
+ return;
145
+ }
146
+ const results = await getMatches(value);
147
+ setPotentialItems(results);
148
+ };
149
+
150
+ return (
151
+ <Autocomplete
152
+ options={potentialItems}
153
+ autoComplete
154
+ includeInputInList
155
+ onInputChange={handleInputChange}
156
+ onChange={(_event, item) => onChange(item)}
157
+ classes={{ input: classes.searchInput }}
158
+ renderInput={params => (
159
+ <TextField label={textInputLabel} variant="outlined" {...params} />
160
+ )}
161
+ getOptionLabel={option => option.label}
162
+ renderOption={(props0, option, state) => (
163
+ [props0, option, getItemLabel, state.index] as React.ReactNode
164
+ )}
165
+ slotProps={{
166
+ listbox: {
167
+ component: ListboxComponent,
168
+ },
169
+ }}
170
+ />
171
+ );
172
+ }
@@ -34,6 +34,11 @@ export { default as Checkbox } from '@mui/material/Checkbox';
34
34
  export { default as Chip } from '@mui/material/Chip';
35
35
  export { default as CircularProgress } from '@mui/material/CircularProgress';
36
36
  export { default as ClickAwayListener } from '@mui/material/ClickAwayListener';
37
+ export { default as Dialog } from '@mui/material/Dialog';
38
+ export { default as DialogActions } from '@mui/material/DialogActions';
39
+ export { default as DialogContent } from '@mui/material/DialogContent';
40
+ export { default as DialogContentText } from '@mui/material/DialogContentText';
41
+ export { default as DialogTitle } from '@mui/material/DialogTitle';
37
42
  export { default as Fade } from '@mui/material/Fade';
38
43
  export { default as FormControl } from '@mui/material/FormControl';
39
44
  export { default as FormControlLabel } from '@mui/material/FormControlLabel';
@@ -121,3 +126,5 @@ export { DataGrid } from '@mui/x-data-grid/DataGrid';
121
126
  // Local exports
122
127
  export { Popper as PopperV4 } from './Popper.js';
123
128
  export { ScopedGlobalStyles } from './ScopedGlobalStyles.js';
129
+ export { SimpleAutocomplete } from './SimpleAutocomplete.js';
130
+ export type { SimpleAutocompleteProps, AutocompleteItem } from './SimpleAutocomplete.js';
@@ -1,6 +1,9 @@
1
1
  // Reference: https://github.com/mui/material-ui/tree/v4.x/packages/material-ui/src/utils
2
2
  import { useMemo } from 'react';
3
3
 
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ type Func = (...args: any[]) => void;
6
+
4
7
  /**
5
8
  * Safe chained function
6
9
  *
@@ -10,40 +13,37 @@ import { useMemo } from 'react';
10
13
  * @param {function} functions to chain
11
14
  * @returns {function|null}
12
15
  */
13
- export function createChainedFunction(...funcs) {
14
- return funcs.reduce(
16
+ export function createChainedFunction(...funcs: (Func | null | undefined)[]): Func {
17
+ return funcs.reduce<Func>(
15
18
  (acc, func) => {
16
19
  if (func == null) {
17
20
  return acc;
18
21
  }
19
22
 
20
- if (process.env.NODE_ENV !== 'production') {
21
- if (typeof func !== 'function') {
22
- console.error(
23
- 'Material-UI: Invalid Argument Type, must only provide functions, undefined, or null.',
24
- );
25
- }
26
- }
27
-
28
- return function chainedFunction(...args) {
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ return function chainedFunction(this: any, ...args: any[]) {
29
25
  acc.apply(this, args);
30
26
  func.apply(this, args);
31
27
  };
32
28
  },
29
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
33
30
  () => {},
34
31
  );
35
32
  }
36
33
 
37
- export function setRef(ref, value) {
34
+ export function setRef<T>(ref: React.Ref<T> | undefined, value: T | null): void {
38
35
  if (typeof ref === 'function') {
39
36
  ref(value);
40
37
  } else if (ref) {
41
38
  // eslint-disable-next-line no-param-reassign
42
- ref.current = value;
39
+ (ref as React.MutableRefObject<T | null>).current = value;
43
40
  }
44
41
  }
45
42
 
46
- export function useForkRef(refA, refB) {
43
+ export function useForkRef<T>(
44
+ refA: React.Ref<T> | undefined,
45
+ refB: React.Ref<T> | undefined,
46
+ ): React.Ref<T> | null {
47
47
  /**
48
48
  * This will create a new function if the ref props change and are defined.
49
49
  * This means react will call the old forkRef with `null` and the new forkRef
@@ -53,7 +53,7 @@ export function useForkRef(refA, refB) {
53
53
  if (refA == null && refB == null) {
54
54
  return null;
55
55
  }
56
- return (refValue) => {
56
+ return (refValue: T) => {
57
57
  setRef(refA, refValue);
58
58
  setRef(refB, refValue);
59
59
  };