@behivetech/organisms.site-header 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ // Converted from site-header.module.scss by `block convert`; this file is the source from here on.
2
+ import { defineBlock } from "@behivetech/blocks.core";
3
+ import { tokens } from "@behivetech/blocks.core/tokens";
4
+ const { color, spacing } = tokens;
5
+ export const siteHeader = defineBlock("BHT__SiteHeader", {
6
+ backgroundColor: color.surface,
7
+ color: color.onSurface,
8
+ borderBottom: [1, "solid", color.outlineVariant],
9
+ children: {
10
+ bar: {
11
+ display: "flex",
12
+ alignItems: "center",
13
+ justifyContent: "space-between",
14
+ gap: spacing[4],
15
+ minHeight: "4rem",
16
+ padding: [spacing[2], spacing[4]],
17
+ web: {
18
+ "@container site-header lg": {
19
+ paddingInline: spacing[8],
20
+ },
21
+ },
22
+ },
23
+ brand: { display: "flex", alignItems: "center", minWidth: 0 },
24
+ end: {
25
+ display: "flex",
26
+ alignItems: "center",
27
+ gap: spacing[2],
28
+ web: { "@container site-header lg": { gap: spacing[6] } },
29
+ },
30
+ nav: {
31
+ display: "none",
32
+ // the native renderer draws the links inline only while the window is wide, so it shows them
33
+ native: { display: "flex", alignItems: "center", gap: spacing[1] },
34
+ web: {
35
+ "@container site-header lg": {
36
+ display: "flex",
37
+ alignItems: "center",
38
+ gap: spacing[1],
39
+ },
40
+ },
41
+ },
42
+ actions: { display: "flex", alignItems: "center" },
43
+ menu: {
44
+ display: "flex",
45
+ web: { "@container site-header lg": { display: "none" } },
46
+ },
47
+ drawerNav: { display: "flex", flexDirection: "column", gap: spacing[2] },
48
+ },
49
+ web: { container: "site-header/inline-size" },
50
+ });
@@ -11,7 +11,7 @@ const LINKS = [
11
11
  /** Brand, four links and no actions. Narrow the header below 64rem to get the menu button and Drawer. */
12
12
  export const BasicSiteHeader = () => (
13
13
  <SiteHeader
14
- brand={<a href="/">The BEhive</a>}
14
+ brand={<a href="/">Northwind</a>}
15
15
  links={LINKS}
16
16
  menuLabel="Menu"
17
17
  />
@@ -20,7 +20,7 @@ export const BasicSiteHeader = () => (
20
20
  /** An action stays in the bar at every breakpoint. */
21
21
  export const SiteHeaderWithActions = () => (
22
22
  <SiteHeader
23
- brand={<a href="/">The BEhive</a>}
23
+ brand={<a href="/">Northwind</a>}
24
24
  links={LINKS}
25
25
  menuLabel="Menu"
26
26
  actions={<Button size="sm">Book direct</Button>}
@@ -0,0 +1,154 @@
1
+ import { Button } from "@behivetech/atoms.button";
2
+ import { Drawer } from "@behivetech/atoms.drawer";
3
+ import { NavItem } from "@behivetech/atoms.nav-item";
4
+ import {
5
+ BREAKPOINTS_REM,
6
+ holdsText,
7
+ REM_DP,
8
+ useBlock,
9
+ type StylesFor,
10
+ } from "@behivetech/blocks.core/native";
11
+ import { MenuIcon } from "@behivetech/icons.menu";
12
+ import { useState, type ReactNode } from "react";
13
+ import {
14
+ Text,
15
+ View,
16
+ useWindowDimensions,
17
+ type StyleProp,
18
+ type ViewProps,
19
+ type ViewStyle,
20
+ } from "react-native";
21
+ import { siteHeader } from "./site-header.block";
22
+ import type { SiteHeaderLink } from "./site-header-types";
23
+
24
+ // The types the web renderer owns, re-exported so the native entry declares the same names. A native file's
25
+ // import of its own stem means the web file in the native declarations (`emitNativeDeclarations`).
26
+ export type { SiteHeaderLink, SiteHeaderLinkProps } from "./site-header-types";
27
+
28
+ export interface SiteHeaderProps extends Omit<ViewProps, "style" | "children"> {
29
+ /** Additional styles, appended after the block's own */
30
+ style?: StyleProp<ViewStyle>;
31
+ /** Brand slot at the start of the bar; rendered as given (a string is drawn in a `Text`), so pass your own pressable brand */
32
+ brand: ReactNode;
33
+ /** Navigation links: inline while the window is at least 1024 dp wide (`lg`), in a `Drawer` when narrower */
34
+ links: SiteHeaderLink[];
35
+ /** Optional actions that stay in the bar at every width, e.g. a booking button */
36
+ actions?: ReactNode;
37
+ /** Called when a link is pressed. A native app has no router, so navigate to the link's `href` here; the `Drawer` closes first */
38
+ onLinkPress?: (link: SiteHeaderLink) => void;
39
+ /** Accessible name of the menu button, the navigation landmarks and the Drawer title, already translated */
40
+ menuLabel: string;
41
+ /** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
42
+ styles?: StylesFor<typeof siteHeader>;
43
+ }
44
+
45
+ /** The window width in dp from which the links sit in the bar: the `lg` breakpoint, which the block's container queries name (the web switches on the header's own width). */
46
+ const WIDE_WIDTH = (BREAKPOINTS_REM.lg as number) * REM_DP;
47
+
48
+ /**
49
+ * The React Native renderer of SiteHeader: the same block as the web one. While the window is at least 1024
50
+ * dp wide the links sit inline; narrower, a menu button opens them in a right-hand `Drawer`. The web switches
51
+ * on the header's own width (a container query), which native has no counterpart of, so this follows the
52
+ * window. A link is a `NavItem` with no `href`: pressing it calls `onLinkPress` (and closes the `Drawer`), and
53
+ * the link marked `current` is announced as selected. There is no `linkComponent`.
54
+ *
55
+ * Safe area: the header is the first thing on the screen, so under a transparent status bar pass the top inset
56
+ * of your safe-area library as padding in `style`; the package depends on none.
57
+ */
58
+ export const SiteHeader = ({
59
+ brand,
60
+ links,
61
+ actions,
62
+ onLinkPress,
63
+ menuLabel,
64
+ style,
65
+ styles,
66
+ ...rest
67
+ }: SiteHeaderProps) => {
68
+ const [rootProps, childProps] = useBlock(siteHeader, {}, { style, styles });
69
+ const [isMenuOpen, setIsMenuOpen] = useState(false);
70
+ const { width } = useWindowDimensions();
71
+ const isWide = width >= WIDE_WIDTH;
72
+ const BrandWrapper = holdsText(brand) ? Text : View;
73
+ const ActionsWrapper = holdsText(actions) ? Text : View;
74
+
75
+ const closeMenu = () => setIsMenuOpen(false);
76
+
77
+ // A menu left open as the window turns wide is closed, so turning back does not reopen it. The width change
78
+ // is noticed while rendering, which React allows for state that follows a prop or a hook's value.
79
+ const [wasWide, setWasWide] = useState(isWide);
80
+
81
+ if (wasWide !== isWide) {
82
+ setWasWide(isWide);
83
+ if (isWide) {
84
+ setIsMenuOpen(false);
85
+ }
86
+ }
87
+ const items = (onPressed?: () => void) =>
88
+ links.map((link) => (
89
+ <NavItem
90
+ key={`${link.href}:${link.label}`}
91
+ active={link.current}
92
+ onPress={() => {
93
+ onPressed?.();
94
+ onLinkPress?.(link);
95
+ }}
96
+ >
97
+ {link.label}
98
+ </NavItem>
99
+ ));
100
+
101
+ return (
102
+ <View role="banner" {...rest} {...rootProps}>
103
+ <View {...childProps("bar")}>
104
+ <BrandWrapper {...childProps("brand")}>{brand}</BrandWrapper>
105
+ <View {...childProps("end")}>
106
+ {isWide ? (
107
+ <View
108
+ {...childProps("nav")}
109
+ role="navigation"
110
+ accessibilityLabel={menuLabel}
111
+ >
112
+ {items()}
113
+ </View>
114
+ ) : null}
115
+ {actions ? (
116
+ <ActionsWrapper {...childProps("actions")}>
117
+ {actions}
118
+ </ActionsWrapper>
119
+ ) : null}
120
+ {isWide ? null : (
121
+ <View {...childProps("menu")}>
122
+ <Button
123
+ variant="ghost"
124
+ size="lg"
125
+ iconOnly
126
+ accessibilityLabel={menuLabel}
127
+ accessibilityState={{ expanded: isMenuOpen }}
128
+ onPress={() => setIsMenuOpen(true)}
129
+ >
130
+ <MenuIcon />
131
+ </Button>
132
+ </View>
133
+ )}
134
+ </View>
135
+ </View>
136
+ {!isWide && isMenuOpen ? (
137
+ <Drawer
138
+ placement="right"
139
+ title={menuLabel}
140
+ padding="sm"
141
+ onClose={closeMenu}
142
+ >
143
+ <View
144
+ {...childProps("drawerNav")}
145
+ role="navigation"
146
+ accessibilityLabel={menuLabel}
147
+ >
148
+ {items(closeMenu)}
149
+ </View>
150
+ </Drawer>
151
+ ) : null}
152
+ </View>
153
+ );
154
+ };
package/site-header.tsx CHANGED
@@ -1,5 +1,5 @@
1
1
  import {
2
- type AnchorHTMLAttributes,
2
+ type ComponentProps,
3
3
  type ElementType,
4
4
  type HTMLAttributes,
5
5
  type ReactNode,
@@ -9,11 +9,18 @@ import {
9
9
  useRef,
10
10
  useState,
11
11
  } from "react";
12
- import { getClassName } from "@behivetech/get-class-name";
13
12
  import { Button } from "@behivetech/atoms.button";
14
13
  import { MenuIcon } from "@behivetech/icons.menu";
15
14
  import { NavItem } from "@behivetech/atoms.nav-item";
16
- import styles from "./site-header.module.scss";
15
+ import "./site-header.block.css";
16
+ import { useBlock, type StylesFor } from "@behivetech/blocks.core";
17
+ import { siteHeader } from "./site-header.block";
18
+ import {
19
+ type SiteHeaderLink,
20
+ type SiteHeaderLinkProps,
21
+ } from "./site-header-types.js";
22
+
23
+ export type { SiteHeaderLink, SiteHeaderLinkProps };
17
24
 
18
25
  /** Loads the Drawer module; repeat calls share the one request. */
19
26
  const loadDrawer = () =>
@@ -32,21 +39,6 @@ const prefetchDrawer = () => {
32
39
  loadDrawer().catch(() => undefined);
33
40
  };
34
41
 
35
- /** One navigation link of the header. */
36
- export interface SiteHeaderLink {
37
- /** Destination of the link */
38
- href: string;
39
- /** Visible text of the link, already translated */
40
- label: string;
41
- /** Marks the link for the current page: renders `aria-current="page"` */
42
- current?: boolean;
43
- }
44
-
45
- /** Props the header passes to `linkComponent` for every link. */
46
- export type SiteHeaderLinkProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
47
- href: string;
48
- };
49
-
50
42
  /** Props of the `SiteHeader` component. */
51
43
  export interface SiteHeaderProps extends Omit<
52
44
  HTMLAttributes<HTMLElement>,
@@ -67,6 +59,8 @@ export interface SiteHeaderProps extends Omit<
67
59
  linkComponent?: ElementType<SiteHeaderLinkProps>;
68
60
  /** Accessible name of the menu button, the navigation landmarks and the Drawer title, already translated */
69
61
  menuLabel: string;
62
+ /** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
63
+ styles?: StylesFor<typeof siteHeader>;
70
64
  }
71
65
 
72
66
  interface HeaderLinksProps {
@@ -98,7 +92,7 @@ interface MenuDrawerProps {
98
92
  links: SiteHeaderLink[];
99
93
  linkComponent: ElementType<SiteHeaderLinkProps>;
100
94
  menuLabel: string;
101
- navClassName: string;
95
+ navProps: ComponentProps<"nav">;
102
96
  onClose: () => void;
103
97
  }
104
98
 
@@ -107,7 +101,7 @@ const MenuDrawer = ({
107
101
  links,
108
102
  linkComponent,
109
103
  menuLabel,
110
- navClassName,
104
+ navProps,
111
105
  onClose,
112
106
  }: MenuDrawerProps) => (
113
107
  <Suspense fallback={null}>
@@ -118,7 +112,7 @@ const MenuDrawer = ({
118
112
  padding="sm"
119
113
  onClose={onClose}
120
114
  >
121
- <nav aria-label={menuLabel} className={navClassName}>
115
+ <nav aria-label={menuLabel} {...navProps}>
122
116
  <HeaderLinks
123
117
  links={links}
124
118
  linkComponent={linkComponent}
@@ -133,6 +127,7 @@ interface HeaderMenuProps {
133
127
  links: SiteHeaderLink[];
134
128
  linkComponent: ElementType<SiteHeaderLinkProps>;
135
129
  menuLabel: string;
130
+ styles?: StylesFor<typeof siteHeader>;
136
131
  }
137
132
 
138
133
  /**
@@ -153,18 +148,20 @@ const useReturnFocus = (isOpen: boolean) => {
153
148
  };
154
149
 
155
150
  /** Menu button and the Drawer it opens; shown only while the header is narrow. */
156
- const HeaderMenu = ({ links, linkComponent, menuLabel }: HeaderMenuProps) => {
151
+ const HeaderMenu = ({
152
+ links,
153
+ linkComponent,
154
+ menuLabel,
155
+ styles,
156
+ }: HeaderMenuProps) => {
157
157
  const [isMenuOpen, setIsMenuOpen] = useState(false);
158
- const [, getChildClass] = getClassName({
159
- rootClass: "BHT__SiteHeader",
160
- styles,
161
- });
158
+ const [, childProps] = useBlock(siteHeader, {}, { styles });
162
159
  const menuButtonRef = useReturnFocus(isMenuOpen);
163
160
 
164
161
  const closeMenu = () => setIsMenuOpen(false);
165
162
 
166
163
  return (
167
- <div className={getChildClass("menu")}>
164
+ <div {...childProps("menu")}>
168
165
  <Button
169
166
  variant="ghost"
170
167
  size="lg"
@@ -196,7 +193,7 @@ const HeaderMenu = ({ links, linkComponent, menuLabel }: HeaderMenuProps) => {
196
193
  links={links}
197
194
  linkComponent={linkComponent}
198
195
  menuLabel={menuLabel}
199
- navClassName={getChildClass("drawer-nav")}
196
+ navProps={childProps("drawerNav")}
200
197
  onClose={closeMenu}
201
198
  />
202
199
  ) : null}
@@ -218,29 +215,29 @@ export const SiteHeader = ({
218
215
  actions,
219
216
  linkComponent = "a",
220
217
  menuLabel,
218
+ styles,
221
219
  ...rest
222
220
  }: SiteHeaderProps) => {
223
- const [rootClass, getChildClass] = getClassName({
224
- className,
225
- rootClass: "BHT__SiteHeader",
226
- styles,
227
- });
221
+ const [rootProps, childProps] = useBlock(
222
+ siteHeader,
223
+ {},
224
+ { className, styles },
225
+ );
228
226
 
229
227
  return (
230
- <header {...rest} className={rootClass}>
231
- <div className={getChildClass("bar")}>
232
- <div className={getChildClass("brand")}>{brand}</div>
233
- <div className={getChildClass("end")}>
234
- <nav aria-label={menuLabel} className={getChildClass("nav")}>
228
+ <header {...rest} {...rootProps}>
229
+ <div {...childProps("bar")}>
230
+ <div {...childProps("brand")}>{brand}</div>
231
+ <div {...childProps("end")}>
232
+ <nav aria-label={menuLabel} {...childProps("nav")}>
235
233
  <HeaderLinks links={links} linkComponent={linkComponent} />
236
234
  </nav>
237
- {actions ? (
238
- <div className={getChildClass("actions")}>{actions}</div>
239
- ) : null}
235
+ {actions ? <div {...childProps("actions")}>{actions}</div> : null}
240
236
  <HeaderMenu
241
237
  links={links}
242
238
  linkComponent={linkComponent}
243
239
  menuLabel={menuLabel}
240
+ styles={styles}
244
241
  />
245
242
  </div>
246
243
  </div>
@@ -1,77 +0,0 @@
1
- @use "@behivetech/cms.base-styles/breakpoints.scss" as breakpoints;
2
-
3
- // Surface bar with a hairline divider. The root is a named inline-size
4
- // container, so the links go inline once the header itself is 64rem wide and
5
- // the menu button opens them in a Drawer below that. A container cannot style
6
- // itself, so the layout lives on the inner `__bar`.
7
- .BHT__SiteHeader {
8
- container: site-header / inline-size;
9
- background: var(--md-sys-color-surface);
10
- color: var(--md-sys-color-on-surface);
11
- border-bottom: 1px solid var(--md-sys-color-outline-variant);
12
-
13
- &__bar {
14
- display: flex;
15
- align-items: center;
16
- justify-content: space-between;
17
- gap: var(--spacing-4, 1rem);
18
- min-height: 4rem;
19
- padding: var(--spacing-2, 0.5rem) var(--spacing-4, 1rem);
20
- }
21
-
22
- &__brand {
23
- display: flex;
24
- align-items: center;
25
- min-width: 0;
26
- }
27
-
28
- &__end {
29
- display: flex;
30
- align-items: center;
31
- gap: var(--spacing-2, 0.5rem);
32
- }
33
-
34
- &__nav {
35
- display: none;
36
- }
37
-
38
- &__actions {
39
- display: flex;
40
- align-items: center;
41
- }
42
-
43
- &__menu {
44
- display: flex;
45
- }
46
-
47
- // Full-width stacked items; the gap keeps their 48px touch targets apart.
48
- &__drawer-nav {
49
- display: flex;
50
- flex-direction: column;
51
- gap: var(--spacing-2, 0.5rem);
52
- }
53
-
54
- // A container query on the header's own width, not a media query (base-styles'
55
- // `lg` mixin): the header can sit in a narrow column of a wide page. The
56
- // threshold is the shared `$breakpoint-lg`. It sits last so it wins over the
57
- // narrow rules of the same elements.
58
- @container site-header (min-width: #{breakpoints.$breakpoint-lg}) {
59
- &__bar {
60
- padding-inline: var(--spacing-8, 2rem);
61
- }
62
-
63
- &__end {
64
- gap: var(--spacing-6, 1.5rem);
65
- }
66
-
67
- &__nav {
68
- display: flex;
69
- align-items: center;
70
- gap: var(--spacing-1, 0.25rem);
71
- }
72
-
73
- &__menu {
74
- display: none;
75
- }
76
- }
77
- }