Added vertical mode for small devices

This commit is contained in:
Jan Zípek 2022-08-24 09:45:54 +02:00
parent ec690ebbcb
commit 508d1630fe
Signed by: kamen
GPG Key ID: A17882625B33AC31
4 changed files with 49 additions and 12 deletions

View File

@ -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,7 +94,13 @@ export const EditableBox = ({
<div <div
ref={boxRef} ref={boxRef}
className={`grid-box${dragging ? ' dragging' : ''}`} className={`grid-box${dragging ? ' dragging' : ''}`}
style={{ style={
verticalMode
? {
position: 'relative',
height: box.h + 'px',
}
: {
left: (box.x / GRID_WIDTH) * 100 + '%', left: (box.x / GRID_WIDTH) * 100 + '%',
top: box.y + 'px', top: box.y + 'px',
width: (box.w / GRID_WIDTH) * 100 + '%', width: (box.w / GRID_WIDTH) * 100 + '%',
@ -102,7 +110,8 @@ export const EditableBox = ({
top: dragging.y, top: dragging.y,
zIndex: 2, zIndex: 2,
}), }),
}} }
}
> >
<div className="box" style={{ height: '100%' }}> <div className="box" style={{ height: '100%' }}>
<div className="header"> <div className="header">

View File

@ -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}>

View File

@ -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

View File

@ -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
}