Skip to content

Commit aeb6f4d

Browse files
authored
Improve data hydration, fix CSS on mobile (#55)
* added better hydration logic, error handling * Added tailwind option to readme, fixed styling on mobile
1 parent f026a62 commit aeb6f4d

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,26 @@ pnpm add ssw.megamenu
1717

1818
2. Import the generated styles
1919

20-
**NOTE: If you have TailwindCSS installed in your project, you can skip this step**
21-
2220
```javascript
23-
import "ssw.megamenu/style.css";
21+
import "ssw.megamenu/dist/style.css";
22+
```
23+
24+
**NOTE: If you have TailwindCSS installed in your project, you can instead list the `ssw.megamenu` package in your `tailwind.config.js` content array like so:**
25+
26+
```js
27+
// tailwind.config.js
28+
29+
// ...
30+
31+
module.exports = {
32+
// ...
33+
content: [
34+
// ...
35+
"node_modules/ssw.megamenu/**/*.js",
36+
],
37+
// ...
38+
};
39+
2440
```
2541

2642
3. You can then use the components in your React app like so:

lib/components/MegaMenuLayout/MegaMenuLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const MegaMenuLayout: React.FC<MegaMenuWrapperProps> = ({
4141

4242
return (
4343
<>
44-
<div className="relative z-10 flex h-16 w-full items-center justify-center sm:h-[120px]">
44+
<div className="relative z-10 flex w-full items-center justify-center sm:h-[120px]">
4545
<nav
4646
className="flex h-full w-full items-center justify-between gap-x-4 overflow-hidden px-0"
4747
aria-label="Global"
@@ -105,7 +105,7 @@ const MegaMenuLayout: React.FC<MegaMenuWrapperProps> = ({
105105
<RightSideActions />
106106
</div>
107107
) : (
108-
<PhoneButton className="pb-4 sm:hidden" />
108+
<PhoneButton className="flex-grow pb-4 sm:hidden" />
109109
)}
110110
</>
111111
);

lib/components/PhoneButton/PhoneButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const PhoneButton = ({ className }: PhoneButtonProps) => {
2121
<CustomLink
2222
href={url}
2323
className={cx(
24-
"flex h-12 w-fit shrink-0 cursor-pointer items-center justify-center rounded-lg bg-ssw-red px-5 text-xl hover:opacity-70",
24+
"flex h-12 w-full shrink-0 cursor-pointer items-center justify-center rounded-lg bg-ssw-red px-5 text-xl hover:opacity-70 max-sm:my-5 sm:w-fit",
2525
)}
2626
>
2727
<FaPhoneAlt color="white" className="text-2xl" />

lib/components/SubMenuGroup/SubMenuWidget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const SubMenuWidget: React.FC<SubMenuWidgetProps> = ({ item }) => {
2626
case "bookNow": {
2727
return (
2828
<CustomLink
29-
className="relative flex w-full cursor-pointer items-center justify-center rounded-md bg-ssw-red font-semibold text-white hover:bg-ssw-light-red"
29+
className="relative flex w-full cursor-pointer items-center justify-center rounded-md bg-ssw-red font-semibold !text-white hover:bg-ssw-light-red"
3030
href={item.url}
3131
>
3232
<MegaIcon icon="phone" className="h-6" />

lib/hooks/useMenuItems.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const API_URL = "https://www.ssw.com.au/api/get-megamenu";
55

66
const refreshData = async () => {
77
const res = await fetch(API_URL);
8-
98
const json = await res.json();
109

1110
const { menuGroups } = json;
@@ -16,16 +15,19 @@ const refreshData = async () => {
1615
export const useMenuItems = (
1716
menuBarItems?: NavMenuGroup[],
1817
): { menuItems: NavMenuGroup[] } => {
19-
const [menuItems, setMenuItems] = useState<NavMenuGroup[] | null>([]);
18+
const [menuItems, setMenuItems] = useState<NavMenuGroup[]>(
19+
menuBarItems || [],
20+
);
2021

2122
useEffect(() => {
22-
if (menuBarItems) {
23-
setMenuItems(menuBarItems);
24-
} else {
25-
refreshData().then((data) => {
23+
refreshData()
24+
.then((data) => {
2625
setMenuItems(data);
26+
})
27+
.catch((err) => {
28+
setMenuItems(menuBarItems || []);
29+
console.error(err);
2730
});
28-
}
2931
}, [menuBarItems]);
3032

3133
return { menuItems: menuItems || [] };

0 commit comments

Comments
 (0)