Added vertical mode for small devices
This commit is contained in:
parent
ec690ebbcb
commit
508d1630fe
|
|
@ -1,6 +1,7 @@
|
||||||
import { useWindowEvent } from '@/utils/hooks/useWindowEvent'
|
import { useWindowEvent } from '@/utils/hooks/useWindowEvent'
|
||||||
import { useRef, useState } from 'preact/hooks'
|
import { useRef, useState } from 'preact/hooks'
|
||||||
import { GRID_WIDTH } from '../constants'
|
import { GRID_WIDTH } from '../constants'
|
||||||
|
import { useDashboardContext } from '../contexts/DashboardContext'
|
||||||
import { useDragging } from '../hooks/useDragging'
|
import { useDragging } from '../hooks/useDragging'
|
||||||
import { ResizingMode, useResize } from '../hooks/useResize'
|
import { ResizingMode, useResize } from '../hooks/useResize'
|
||||||
import { BoxDefinition } from '../types'
|
import { BoxDefinition } from '../types'
|
||||||
|
|
@ -23,6 +24,7 @@ export const EditableBox = ({
|
||||||
onEdit,
|
onEdit,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const boxRef = useRef<HTMLDivElement>(null)
|
const boxRef = useRef<HTMLDivElement>(null)
|
||||||
|
const { verticalMode } = useDashboardContext()
|
||||||
|
|
||||||
const [editing, setEditing] = useState(false)
|
const [editing, setEditing] = useState(false)
|
||||||
|
|
||||||
|
|
@ -92,17 +94,24 @@ export const EditableBox = ({
|
||||||
<div
|
<div
|
||||||
ref={boxRef}
|
ref={boxRef}
|
||||||
className={`grid-box${dragging ? ' dragging' : ''}`}
|
className={`grid-box${dragging ? ' dragging' : ''}`}
|
||||||
style={{
|
style={
|
||||||
left: (box.x / GRID_WIDTH) * 100 + '%',
|
verticalMode
|
||||||
top: box.y + 'px',
|
? {
|
||||||
width: (box.w / GRID_WIDTH) * 100 + '%',
|
position: 'relative',
|
||||||
height: box.h + 'px',
|
height: box.h + 'px',
|
||||||
...(dragging.active && {
|
}
|
||||||
left: dragging.x,
|
: {
|
||||||
top: dragging.y,
|
left: (box.x / GRID_WIDTH) * 100 + '%',
|
||||||
zIndex: 2,
|
top: box.y + 'px',
|
||||||
}),
|
width: (box.w / GRID_WIDTH) * 100 + '%',
|
||||||
}}
|
height: box.h + 'px',
|
||||||
|
...(dragging.active && {
|
||||||
|
left: dragging.x,
|
||||||
|
top: dragging.y,
|
||||||
|
zIndex: 2,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<div className="box" style={{ height: '100%' }}>
|
<div className="box" style={{ height: '100%' }}>
|
||||||
<div className="header">
|
<div className="header">
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { useViewportSize } from '@/utils/hooks/useViewportSize'
|
||||||
import { intervalToRange } from '@/utils/intervalToRange'
|
import { intervalToRange } from '@/utils/intervalToRange'
|
||||||
import { ComponentChild, createContext } from 'preact'
|
import { ComponentChild, createContext } from 'preact'
|
||||||
import { StateUpdater, useContext, useMemo, useState } from 'preact/hooks'
|
import { StateUpdater, useContext, useMemo, useState } from 'preact/hooks'
|
||||||
|
|
@ -6,6 +7,7 @@ import { FilterValue } from '../components/Filters'
|
||||||
type DashboardContextType = {
|
type DashboardContextType = {
|
||||||
filter: FilterValue
|
filter: FilterValue
|
||||||
setFilter: StateUpdater<FilterValue>
|
setFilter: StateUpdater<FilterValue>
|
||||||
|
verticalMode: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const DashboardContext = createContext<DashboardContextType | null>(null)
|
const DashboardContext = createContext<DashboardContextType | null>(null)
|
||||||
|
|
@ -15,13 +17,20 @@ export const DashboardContextProvider = ({
|
||||||
}: {
|
}: {
|
||||||
children: ComponentChild
|
children: ComponentChild
|
||||||
}) => {
|
}) => {
|
||||||
|
const viewport = useViewportSize()
|
||||||
|
|
||||||
const [filter, setFilter] = useState<FilterValue>(() => {
|
const [filter, setFilter] = useState<FilterValue>(() => {
|
||||||
const range = intervalToRange('week', new Date(), new Date())
|
const range = intervalToRange('week', new Date(), new Date())
|
||||||
|
|
||||||
return { interval: 'week', customFrom: range[0], customTo: range[1] }
|
return { interval: 'week', customFrom: range[0], customTo: range[1] }
|
||||||
})
|
})
|
||||||
|
|
||||||
const value = useMemo(() => ({ filter, setFilter }), [filter])
|
const verticalMode = viewport.width < 800
|
||||||
|
|
||||||
|
const value = useMemo(
|
||||||
|
() => ({ filter, setFilter, verticalMode }),
|
||||||
|
[filter, verticalMode]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DashboardContext.Provider value={value}>
|
<DashboardContext.Provider value={value}>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
import { BoxDefinition } from '../types'
|
import { BoxDefinition } from '../types'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes sure the boxes stick to the top and also that the array
|
||||||
|
* is sorted by y coordinate
|
||||||
|
*/
|
||||||
export function normalizeBoxes(boxes: BoxDefinition[]) {
|
export function normalizeBoxes(boxes: BoxDefinition[]) {
|
||||||
let sorted = false
|
let sorted = false
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { useState } from 'preact/hooks'
|
||||||
|
import { useWindowEvent } from './useWindowEvent'
|
||||||
|
|
||||||
|
const getSize = () => ({
|
||||||
|
width: window.innerWidth,
|
||||||
|
height: window.innerHeight,
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useViewportSize = () => {
|
||||||
|
const [size, setSize] = useState(getSize())
|
||||||
|
|
||||||
|
useWindowEvent('resize', () => setSize(getSize()))
|
||||||
|
|
||||||
|
return size
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue