@tanstack/react-query-devtools 4.10.3 → 4.11.0

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/utils.ts CHANGED
@@ -154,3 +154,165 @@ export const sortFns: Record<string, SortFn> = {
154
154
  'Query Hash': queryHashSort,
155
155
  'Last Updated': dateSort,
156
156
  }
157
+
158
+ export const minPanelSize = 70
159
+ export const defaultPanelSize = 500
160
+ export const sides: Record<Side, Side> = {
161
+ top: 'bottom',
162
+ bottom: 'top',
163
+ left: 'right',
164
+ right: 'left',
165
+ }
166
+
167
+ export type Corner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
168
+ export type Side = 'left' | 'right' | 'top' | 'bottom'
169
+ /**
170
+ * Check if the given side is vertical (left/right)
171
+ */
172
+ export function isVerticalSide(side: Side) {
173
+ return ['left', 'right'].includes(side)
174
+ }
175
+ /**
176
+ * Get the opposite side, eg 'left' => 'right'. 'top' => 'bottom', etc
177
+ */
178
+ export function getOppositeSide(side: Side): Side {
179
+ return sides[side]
180
+ }
181
+ /**
182
+ * Given as css prop it will return a sided css prop based on a given side
183
+ * Example given `border` and `right` it return `borderRight`
184
+ */
185
+ export function getSidedProp<T extends string>(prop: T, side: Side) {
186
+ return `${prop}${
187
+ side.charAt(0).toUpperCase() + side.slice(1)
188
+ }` as `${T}${Capitalize<Side>}`
189
+ }
190
+
191
+ export interface SidePanelStyleOptions {
192
+ /**
193
+ * Position of the panel
194
+ * Defaults to 'bottom'
195
+ */
196
+ position?: Side
197
+ /**
198
+ * Staring height for the panel, it is set if the position is horizontal eg 'top' or 'bottom'
199
+ * Defaults to 500
200
+ */
201
+ height?: number
202
+ /**
203
+ * Staring width for the panel, it is set if the position is vertical eg 'left' or 'right'
204
+ * Defaults to 500
205
+ */
206
+ width?: number
207
+ /**
208
+ * RQ devtools theme
209
+ */
210
+ devtoolsTheme: Theme
211
+ /**
212
+ * Sets the correct transition and visibility styles
213
+ */
214
+ isOpen?: boolean
215
+ /**
216
+ * If the panel is resizing set to true to apply the correct transition styles
217
+ */
218
+ isResizing?: boolean
219
+ /**
220
+ * Extra panel style passed by the user
221
+ */
222
+ panelStyle?: React.CSSProperties
223
+ }
224
+
225
+ export function getSidePanelStyle({
226
+ position = 'bottom',
227
+ height,
228
+ width,
229
+ devtoolsTheme,
230
+ isOpen,
231
+ isResizing,
232
+ panelStyle,
233
+ }: SidePanelStyleOptions): React.CSSProperties {
234
+ const oppositeSide = getOppositeSide(position)
235
+ const borderSide = getSidedProp('border', oppositeSide)
236
+ const isVertical = isVerticalSide(position)
237
+
238
+ return {
239
+ ...panelStyle,
240
+ direction: 'ltr',
241
+ position: 'fixed',
242
+ [position]: 0,
243
+ [borderSide]: `1px solid ${devtoolsTheme.gray}`,
244
+ transformOrigin: oppositeSide,
245
+ boxShadow: '0 0 20px rgba(0,0,0,.3)',
246
+ zIndex: 99999,
247
+ // visibility will be toggled after transitions, but set initial state here
248
+ visibility: isOpen ? 'visible' : 'hidden',
249
+ ...(isResizing
250
+ ? {
251
+ transition: `none`,
252
+ }
253
+ : { transition: `all .2s ease` }),
254
+ ...(isOpen
255
+ ? {
256
+ opacity: 1,
257
+ pointerEvents: 'all',
258
+ transform: isVertical
259
+ ? `translateX(0) scale(1)`
260
+ : `translateY(0) scale(1)`,
261
+ }
262
+ : {
263
+ opacity: 0,
264
+ pointerEvents: 'none',
265
+ transform: isVertical
266
+ ? `translateX(15px) scale(1.02)`
267
+ : `translateY(15px) scale(1.02)`,
268
+ }),
269
+ ...(isVertical
270
+ ? {
271
+ top: 0,
272
+ height: '100vh',
273
+ maxWidth: '90%',
274
+ width:
275
+ typeof width === 'number' && width >= minPanelSize
276
+ ? width
277
+ : defaultPanelSize,
278
+ }
279
+ : {
280
+ left: 0,
281
+ width: '100%',
282
+ maxHeight: '90%',
283
+ height:
284
+ typeof height === 'number' && height >= minPanelSize
285
+ ? height
286
+ : defaultPanelSize,
287
+ }),
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Get resize handle style based on a given side
293
+ */
294
+ export function getResizeHandleStyle(
295
+ position: Side = 'bottom',
296
+ ): React.CSSProperties {
297
+ const isVertical = isVerticalSide(position)
298
+ const oppositeSide = getOppositeSide(position)
299
+ const marginSide = getSidedProp('margin', oppositeSide)
300
+
301
+ return {
302
+ position: 'absolute',
303
+ cursor: isVertical ? 'col-resize' : 'row-resize',
304
+ zIndex: 100000,
305
+ [oppositeSide]: 0,
306
+ [marginSide]: `-4px`,
307
+ ...(isVertical
308
+ ? {
309
+ top: 0,
310
+ height: '100%',
311
+ width: '4px',
312
+ }
313
+ : {
314
+ width: '100%',
315
+ height: '4px',
316
+ }),
317
+ }
318
+ }