-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from NIAEFEUP/feature/switchSidebarPosition
Feature: Button that allows the user to switch the side where the sidebar is displayed
- Loading branch information
Showing
7 changed files
with
125 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createContext, useContext, useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
type SidebarContextType = { | ||
sidebarPosition: 'left' | 'right'; | ||
toggleSidebarPosition: () => void; | ||
}; | ||
|
||
const SidebarContext = createContext<SidebarContextType | undefined>(undefined); | ||
|
||
export const SidebarProvider = ({ children }: { children: JSX.Element }) => { | ||
const [sidebarPosition, setSidebarPosition] = useState<'left' | 'right'>(() => { | ||
const storedPosition = window.localStorage.getItem("sidebar-position"); | ||
return storedPosition === "left" || storedPosition === "right" ? storedPosition : "right"; | ||
}); | ||
|
||
const toggleSidebarPosition = () => { | ||
setSidebarPosition((prev) => { | ||
const newPosition = prev === "right" ? "left" : "right"; | ||
window.localStorage.setItem("sidebar-position", newPosition); | ||
return newPosition; | ||
}); | ||
}; | ||
|
||
return ( | ||
<SidebarContext.Provider value={{ sidebarPosition, toggleSidebarPosition }}> | ||
{children} | ||
</SidebarContext.Provider> | ||
); | ||
}; | ||
|
||
SidebarProvider.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
} | ||
|
||
export const useSidebarContext = () => { | ||
const context = useContext(SidebarContext); | ||
if (!context) { | ||
throw new Error('useSidebarContext must be used within a SidebarProvider') | ||
} | ||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters