Skip to content

Commit

Permalink
added grayscale toggle functionality using useContext
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskimty committed Mar 15, 2023
1 parent f7dfd1a commit a13d1d2
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 146 deletions.
Binary file modified public/favicon.ico
Binary file not shown.
Binary file modified public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Routes, Route } from 'react-router-dom';
import { ToggleGrayscale } from './context/ToggleGrayscale';
import NavBar from './components/NavBar';
import LandingPage from './components/LandingPage';
import CategoryPage from './components/CategoryPage';
Expand All @@ -11,14 +12,16 @@ import './styles/styles.scss';
const App = () => {
return (
<div className="App">
<NavBar />
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/category/:name" element={<CategoryPage />} />
<Route path="/product/:name" element={<ProductPage />} />
<Route path="/about-the-creators" element={<TheCreators />} />
</Routes>
<Footer />
<ToggleGrayscale>
<NavBar />
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/category/:name" element={<CategoryPage />} />
<Route path="/product/:name" element={<ProductPage />} />
<Route path="/about-the-creators" element={<TheCreators />} />
</Routes>
<Footer />
</ToggleGrayscale>
</div>
);
}
Expand Down
29 changes: 19 additions & 10 deletions src/components/CategoryCard.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { Link } from 'react-router-dom';
import placeholderImg from '../assets/LandingPage/Lips.jpg'
import placeholderImg from '../assets/LandingPage/Lips.jpg';
import { useGray } from "../context/ToggleGrayscale";
const CategoryCard = ({ categories }) => {
const category_cards = categories.map((cat) => (
<div key={cat._id} id={cat.name} className="catCards">
<Link to={"/category/" + cat.name.toLowerCase()} state={cat}>
{/* placeholder images for now */}
<img src={placeholderImg} alt={cat.name} className="catCardsImg" />
<h3>{cat.name}</h3>
</Link>
</div>
));

const { isActive } = useGray();

const category_cards = categories.map((cat) => (
<div key={cat._id} id={cat.name} className="catCards">
<Link to={"/category/" + cat.name.toLowerCase()} state={cat}>
{/* placeholder images for now */}
<img
src={placeholderImg}
alt={cat.name}
className="catCardsImg"
style={{ filter: isActive ? "grayscale(100%)" : "none" }}
/>
<h3>{cat.name}</h3>
</Link>
</div>
));

return (
<div className="categoryCard">
Expand Down
3 changes: 2 additions & 1 deletion src/components/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const NavBar = () => {
</li> */}
{/* in MOBILE, bring it BELOW logo */}
<li className="toggle">
Greyscale
{/* maybe have text switch between Grayscale and Color? In that case might need to bring the handleChange into this component */}
Grayscale
<Toggle />
</li>
</ul>
Expand Down
105 changes: 0 additions & 105 deletions src/components/PureColor.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/components/PureColorEnvy.js

This file was deleted.

23 changes: 14 additions & 9 deletions src/components/Toggle.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import Switch from "react-switch";
import { useState } from "react";
import { useGray } from "../context/ToggleGrayscale";

const Toggle = () => {
const { handleGray } = useGray();
const [checked, setChecked] = useState(false);

const handleChange = (nextChecked) => {
setChecked(nextChecked);
handleGray();
};

return (
<div className="switch">
{/* https://github.com/markusenglund/react-switch#readme */}
<label>
<Switch
onChange={handleChange}
checked={checked}
onColor="#000"
className="reactSwitch"
/>
</label>
{/* https://github.com/markusenglund/react-switch#readme */}
<label>
<span className="sr-only">Switch to toggle Greyscale</span>
<Switch
onChange={handleChange}
checked={checked}
onColor="#000"
className="reactSwitch"
/>
</label>
</div>
);
};
Expand Down
22 changes: 22 additions & 0 deletions src/context/ToggleGrayscale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createContext, useContext, useState } from "react";

const GrayScaleContext = createContext();

export function ToggleGrayscale({ children }) {
const [isActive, setIsActive] = useState(false);
const handleGray = () => {
setIsActive((current) => !current);
}

return (
<GrayScaleContext.Provider
value={{ isActive, setIsActive, handleGray }}
>
{children}
</GrayScaleContext.Provider>
);
};

export function useGray() {
return useContext(GrayScaleContext);
}

0 comments on commit a13d1d2

Please sign in to comment.