|
| 1 | +"use client"; |
| 2 | +import { useState, useEffect } from "react"; |
| 3 | +import Stack, { onEntryChange } from "../../lib/index"; |
| 4 | +import { Dialog } from "@headlessui/react"; |
| 5 | +import { |
| 6 | + Bars3Icon, |
| 7 | + XMarkIcon, |
| 8 | + InformationCircleIcon, |
| 9 | +} from "@heroicons/react/24/outline"; |
| 10 | + |
| 11 | +export default function NavBar() { |
| 12 | + const [mobileMenuOpen, setMobileMenuOpen] = useState(false); |
| 13 | + const [entry, setEntry] = useState({}); |
| 14 | + const [loading, SetLoading] = useState(true); |
| 15 | + |
| 16 | + const getContent = async () => { |
| 17 | + const entry = await Stack.getElementByTypeWtihRefs( |
| 18 | + "menu", |
| 19 | + [""] |
| 20 | + ); |
| 21 | + console.log("header:", entry[0][0]); |
| 22 | + setEntry(entry[0][0]); |
| 23 | + SetLoading(false); |
| 24 | + }; |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + onEntryChange(getContent); |
| 28 | + }, []); |
| 29 | + |
| 30 | + if (loading) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <header className="bg-white shadow-md"> |
| 36 | + <nav className="flex items-center justify-between p-6 lg:px-8"> |
| 37 | + <div className="flex lg:flex-1"> |
| 38 | + <a href="/" className="-m-1.5 p-1.5"> |
| 39 | + |
| 40 | + {/* ----- LOGO IMAGE SERVED FROM CONTENTSTACK ----- */} |
| 41 | + <img |
| 42 | + className="h-8 w-auto" |
| 43 | + src={entry?.logo?.url} |
| 44 | + alt="" |
| 45 | + {...entry?.$?.logo} |
| 46 | + /> |
| 47 | + |
| 48 | + </a> |
| 49 | + </div> |
| 50 | + <div className="flex lg:hidden"> |
| 51 | + <button |
| 52 | + type="button" className="-m-2.5 inline-flex items-center justify-center rounded-md p-2.5 text-gray-700" |
| 53 | + onClick={() => setMobileMenuOpen(true)} |
| 54 | + > |
| 55 | + <Bars3Icon className="h-6 w-6" aria-hidden="true" /> |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + <div className="hidden lg:flex lg:gap-x-12"> |
| 59 | + |
| 60 | + {/* ----- ITTERATE OVER ARRAY FROM CONTENTSTACK ----- */} |
| 61 | + {entry.menu_items?.map((item) => ( |
| 62 | + <a |
| 63 | + key={item.menu_item.title} |
| 64 | + href={item.menu_item.href} |
| 65 | + className="text-sm font-semibold leading-6 text-gray-900" |
| 66 | + {...item?.menu_item?.$?.title} |
| 67 | + > |
| 68 | + {item.menu_item.title} |
| 69 | + </a> |
| 70 | + ))} |
| 71 | + |
| 72 | + </div> |
| 73 | + <div className="hidden lg:flex lg:flex-1 lg:justify-end"> |
| 74 | + <a |
| 75 | + href="https://www.contentstack.com/docs/developers" |
| 76 | + target="__blank" |
| 77 | + > |
| 78 | + <InformationCircleIcon |
| 79 | + className="h-6 w-6 flex-none text-neutral-800" |
| 80 | + /> |
| 81 | + </a> |
| 82 | + </div> |
| 83 | + </nav> |
| 84 | + <Dialog |
| 85 | + as="div" |
| 86 | + className="lg:hidden" |
| 87 | + open={mobileMenuOpen} |
| 88 | + onClose={setMobileMenuOpen} |
| 89 | + > |
| 90 | + <div className="fixed inset-0 z-10" /> |
| 91 | + <Dialog.Panel className="fixed inset-y-0 right-0 z-10 w-full overflow-y-auto bg-white px-6 py-6 sm:max-w-sm sm:ring-1 sm:ring-gray-900/10"> |
| 92 | + <div className="flex items-center justify-between"> |
| 93 | + |
| 94 | + {/* ----- LOGO FROM CONTENTSTACK FOR MOBILE MENU ----- */} |
| 95 | + <a href="#" className="-m-1.5 p-1.5"> |
| 96 | + <img className="h-8 w-auto" src={entry?.logo?.url} alt="" /> |
| 97 | + </a> |
| 98 | + <button |
| 99 | + type="button" |
| 100 | + className="-m-2.5 rounded-md p-2.5 text-gray-700" |
| 101 | + onClick={() => setMobileMenuOpen(false)} |
| 102 | + > |
| 103 | + <XMarkIcon className="h-6 w-6" aria-hidden="true" /> |
| 104 | + </button> |
| 105 | + </div> |
| 106 | + <div className="mt-6 flow-root"> |
| 107 | + <div className="-my-6 "> |
| 108 | + <div className="space-y-2 py-6"> |
| 109 | + |
| 110 | + {/* ----- ITTERATE OVER ARRAY FROM CONTENTSTACK FOR MOBILE MENU ----- */} |
| 111 | + {entry?.menu_item?.map((item) => ( |
| 112 | + <a |
| 113 | + key={item.title} |
| 114 | + href={item.href} |
| 115 | + className="-mx-3 block rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50" |
| 116 | + {...item?.$?.title} |
| 117 | + > |
| 118 | + {item.title} |
| 119 | + </a> |
| 120 | + ))} |
| 121 | + <a |
| 122 | + key={"docs"} |
| 123 | + href="https://www.contentstack.com/docs/developers" |
| 124 | + className="-mx-3 block rounded-lg px-3 py-2 text-base font-semibold leading-7 text-gray-900 hover:bg-gray-50" |
| 125 | + > |
| 126 | + Documentation |
| 127 | + </a> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </Dialog.Panel> |
| 132 | + </Dialog> |
| 133 | + </header> |
| 134 | + ); |
| 135 | +} |
0 commit comments