Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Dra ut window.innerWidth til en provider for gjenbruk av state
Browse files Browse the repository at this point in the history
  • Loading branch information
jstnhlj committed Oct 19, 2023
1 parent 7a8fff1 commit e1a7474
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
34 changes: 34 additions & 0 deletions src/contexts/window-inner-width.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createContext, ReactNode, useContext, useEffect, useState } from 'react';

interface WindowInnerWidthProps {
innerWidth: number;
}

const WindowInnerWidthContext = createContext<WindowInnerWidthProps>({ innerWidth: window.innerWidth });
function WindowInnerWidthProvider(props: { children: ReactNode }) {
const [innerWidth, setInnerWidth] = useState<number>(window.innerWidth);

useEffect(() => {
const updateInnerWidth = () => {
setInnerWidth(window.innerWidth);
};

window.addEventListener('resize', updateInnerWidth);

return () => window.removeEventListener('resize', updateInnerWidth);
}, []);

return <WindowInnerWidthContext.Provider value={{ innerWidth }}>{props.children}</WindowInnerWidthContext.Provider>;
}

function useWindowInnerWidth() {
const context = useContext(WindowInnerWidthContext);

if (context === undefined) {
throw new Error('useWindowInnerWidth må brukes under en WindowInnerWidthProvider');
}

return context;
}

export { WindowInnerWidthProvider, useWindowInnerWidth };
5 changes: 4 additions & 1 deletion src/main-standard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ReaktiveringProvider } from './contexts/reaktivering';
import { BesvarelseProvider } from './contexts/besvarelse';
import { MeldepliktProvider } from './contexts/meldeplikt';
import StandardWrapper from './standard-wrapper';
import { WindowInnerWidthProvider } from './contexts/window-inner-width';

const Mikrofrontend = () => {
const [state, setState] = React.useState<Autentisering.State>(Autentisering.initialState);
Expand Down Expand Up @@ -67,7 +68,9 @@ const Mikrofrontend = () => {
<MeldepliktProvider>
<ReaktiveringProvider>
<BesvarelseProvider>
<StandardWrapper />
<WindowInnerWidthProvider>
<StandardWrapper />
</WindowInnerWidthProvider>
</BesvarelseProvider>
</ReaktiveringProvider>
</MeldepliktProvider>
Expand Down
14 changes: 2 additions & 12 deletions src/standard-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { useEffect, useState } from 'react';
import flex from './flex.module.css';
import AiaTabs from './tabs/aia-tabs';
import InnholdStandard from './innhold/innhold-standard';
import { useWindowInnerWidth } from './contexts/window-inner-width';

const MIN_TABS_BREDDE = 666;
function StandardWrapper() {
const [innerWidth, setInnerWidth] = useState<number>(window.innerWidth);

useEffect(() => {
const updateInnerWidth = () => {
setInnerWidth(window.innerWidth);
};

window.addEventListener('resize', updateInnerWidth);

return () => window.removeEventListener('resize', updateInnerWidth);
}, []);
const { innerWidth } = useWindowInnerWidth();

const brukTabs = innerWidth > MIN_TABS_BREDDE;

Expand Down

0 comments on commit e1a7474

Please sign in to comment.