Skip to content

Commit

Permalink
Made static InstitutionsTabsList
Browse files Browse the repository at this point in the history
Refactor some stuf:
- Tab, TabList added index.js with exports
- Separated chakra theme styling: tabStyle, institutionTabStyle. They are combined in ChakraTheme.js
  • Loading branch information
skorphil committed Feb 5, 2024
1 parent 1a1cd75 commit 7d46704
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 43 deletions.
29 changes: 17 additions & 12 deletions app/ChakraTheme.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
// theme.js

// 1. import `extendTheme` function
import { extendTheme } from "@chakra-ui/react";
import { tabsTheme } from "../components/InstitutionTab/Tabs.chakra";

// 2. Add your color mode config
import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react";
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(["tablist", "tab"]);
import { institutionTabStyle } from "../components/InstitutionTab";
import { InstitutionsTabsListStyle } from "../components/InstitutionsTabsList/InstitutionTabsList.chakra";

const tabsVariant = defineMultiStyleConfig({
variants: {
grid: {
tab: institutionTabStyle,
tablist: InstitutionsTabsListStyle,
},
},
});

const config = {
initialColorMode: "dark",
useSystemColorMode: false,
};

const tab = {
// how to style complex components?
};

// 3. extend the theme
const theme = extendTheme({
config,
components: {
Tabs: tabsTheme,
Tabs: tabsVariant,
},
});
console.log(theme);
// console.log(theme);
export default theme;
10 changes: 10 additions & 0 deletions components/InstitutionTab/InstitutionTab.chakra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const institutionTabStyle = {
borderRadius: "base",
_selected: {
bg: "black",
},
justifyContent: "space-between",
_disabled: {
opacity: "1",
},
};
4 changes: 1 addition & 3 deletions components/InstitutionTab/InstitutionTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IconButton, Tab, forwardRef, Box } from "@chakra-ui/react";
import { CgUndo } from "react-icons/cg";
import classes from "./InstitutionTab.module.css";

const IntitutionTab = forwardRef(
export const InstitutionTab = forwardRef(
(
{ name = "Unnamed institution", isDeleted = false, state = null, ...props },
ref
Expand Down Expand Up @@ -37,5 +37,3 @@ function TabRightSection({ state, isDeleted }) {
);
} else return false;
}

export default IntitutionTab;
2 changes: 1 addition & 1 deletion components/InstitutionTab/InstitutionTab.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import InstitutionTab from "./InstitutionTab";
import { InstitutionTab } from "./InstitutionTab";
import { Tabs, TabList } from "@chakra-ui/react";

export default {
Expand Down
27 changes: 0 additions & 27 deletions components/InstitutionTab/Tabs.chakra.js

This file was deleted.

2 changes: 2 additions & 0 deletions components/InstitutionTab/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { InstitutionTab } from "./InstitutionTab";
export { institutionTabStyle } from "./InstitutionTab.chakra";
3 changes: 3 additions & 0 deletions components/InstitutionsTabsList/InstitutionTabsList.chakra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const InstitutionsTabsListStyle = {
display: "grid",
};
87 changes: 87 additions & 0 deletions components/InstitutionsTabsList/InstitutionsTabsList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use client";

import { InstitutionTab } from "../InstitutionTab";
import {
Tabs,
TabList,
Heading,
Button,
Box,
IconButton,
} from "@chakra-ui/react";
import { useLayoutEffect, useState } from "react";
import classes from "./InstitutionsTabsList.module.css";
import { CgMathPlus } from "react-icons/cg";

export default function InstitutionsTabsList({
institutions,
simulateKeyboard = false,
}) {
const { height } = useVisualViewportSize();
const isKeyboardOpened =
simulateKeyboard || (window.innerHeight - height > 200 ? true : false);

return (
<>
<Tabs variant="grid" my={isKeyboardOpened || 3}>
{isKeyboardOpened || <Heading>Institutions</Heading>}
<Box
overflowX={isKeyboardOpened && "auto"}
px={isKeyboardOpened && 2}
py={2}
>
<TabList
key="tablist"
className={`${classes.grid} ${isKeyboardOpened ? classes.collapsed : classes.expanded}`}
>
{institutions.map((institution, id) => (
<InstitutionTab
width={isKeyboardOpened && "180px"}
key={`institutionTab-${id}`}
name={institution.name}
state="new"
/>
))}
{isKeyboardOpened
? newInstitutionButtonCollapsed
: newInstitutionButtonExpanded}
</TabList>
</Box>
</Tabs>
</>
);
}

const newInstitutionButtonExpanded = (
<Button variant="outline">
<Box as="span" overflow="hidden">
Add institution
</Box>
</Button>
);

const newInstitutionButtonCollapsed = (
<IconButton
variant="outline"
aria-label="Add institution"
icon={<CgMathPlus />}
/>
);

function useVisualViewportSize() {
// TODO make more abstracted logic, which can distinguish between mobile device and tabs
const [visualViewport, setVisualViewport] = useState([0, 0]);
useLayoutEffect(() => {
function updateVisualViewportSize() {
setVisualViewport({
width: window.visualViewport.width,
height: window.visualViewport.height,
});
}
window.visualViewport.addEventListener("resize", updateVisualViewportSize);
updateVisualViewportSize();
return () => window.removeEventListener("resize", updateVisualViewportSize);
}, []);

return visualViewport;
}
14 changes: 14 additions & 0 deletions components/InstitutionsTabsList/InstitutionsTabsList.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.grid {
gap: var(--chakra-sizes-3)
/* display property is overriden with flex by chakra theme,
so specified grid there (InstitutionTabsList.chakra.js) */
}

.expanded {
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}

.collapsed {
grid-auto-flow: column;
width: fit-content;
}
42 changes: 42 additions & 0 deletions components/InstitutionsTabsList/InstitutionsTabsList.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import InstitutionsTabsList from "./InstitutionsTabsList";

export default {
title: "RecordForm/InstitutionsTabsList",
component: InstitutionsTabsList,
decorators: [
(Story) => (
<>
<Story />
<input type="text" placeholder="test keyboard" />
</>
),
],
};

export const Default = {
args: {
simulateKeyboard: false,
institutions: [
{
name: "City Bank",
country: "it",
assets: [],
},
{
name: "Wells & Fargo",
country: "",
assets: [],
},
{
name: "Bank Of America",
country: "",
assets: [],
},
{
name: "Raiffeisen Bank",
country: "",
assets: [],
},
],
},
};
2 changes: 2 additions & 0 deletions components/InstitutionsTabsList/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { InstitutionsTabsList } from "./InstitutionsTabsList";
export { InstitutionsTabsListStyle } from "./InstitutionTabsList.chakra";

0 comments on commit 7d46704

Please sign in to comment.