|
| 1 | +--- |
| 2 | +import SidebarRestorePoint from './SidebarRestorePoint.astro'; |
| 3 | +import { Badge, Icon } from '@astrojs/starlight/components'; |
| 4 | +import type { SidebarEntry } from '@astrojs/starlight/utils/routing/types'; |
| 5 | +
|
| 6 | +interface Props { |
| 7 | + sublist: SidebarEntry[]; |
| 8 | + nested?: boolean; |
| 9 | +} |
| 10 | +
|
| 11 | +const { sublist, nested } = Astro.props; |
| 12 | +const isExternalHref = (href: string) => /^https?:\/\//.test(href); |
| 13 | +const hasCurrentChild = (entries: SidebarEntry[]): boolean => |
| 14 | + entries.some((entry) => |
| 15 | + entry.type === 'link' ? entry.isCurrent : hasCurrentChild(entry.entries) |
| 16 | + ); |
| 17 | +--- |
| 18 | + |
| 19 | +<ul class:list={{ 'top-level': !nested }}> |
| 20 | + { |
| 21 | + sublist.map((entry) => ( |
| 22 | + <li> |
| 23 | + {entry.type === 'link' ? ( |
| 24 | + <a |
| 25 | + href={entry.href} |
| 26 | + aria-current={entry.isCurrent && 'page'} |
| 27 | + class:list={[{ large: !nested }, entry.attrs.class]} |
| 28 | + {...entry.attrs} |
| 29 | + > |
| 30 | + <span>{entry.label}</span> |
| 31 | + {entry.badge && ( |
| 32 | + <Badge |
| 33 | + variant={entry.badge.variant} |
| 34 | + class={entry.badge.class} |
| 35 | + text={entry.badge.text} |
| 36 | + /> |
| 37 | + )} |
| 38 | + {isExternalHref(entry.href) && ( |
| 39 | + <Icon name="external" class="nav-external-icon" size="0.9rem" /> |
| 40 | + )} |
| 41 | + </a> |
| 42 | + ) : ( |
| 43 | + <details open={hasCurrentChild(entry.entries) || !entry.collapsed}> |
| 44 | + <SidebarRestorePoint /> |
| 45 | + <summary> |
| 46 | + <span class="group-label"> |
| 47 | + <span class="large">{entry.label}</span> |
| 48 | + {entry.badge && ( |
| 49 | + <Badge |
| 50 | + variant={entry.badge.variant} |
| 51 | + class={entry.badge.class} |
| 52 | + text={entry.badge.text} |
| 53 | + /> |
| 54 | + )} |
| 55 | + </span> |
| 56 | + <Icon name="right-caret" class="caret" size="1.25rem" /> |
| 57 | + </summary> |
| 58 | + <Astro.self sublist={entry.entries} nested /> |
| 59 | + </details> |
| 60 | + )} |
| 61 | + </li> |
| 62 | + )) |
| 63 | + } |
| 64 | +</ul> |
| 65 | + |
| 66 | +<style> |
| 67 | + @layer starlight.core { |
| 68 | + ul { |
| 69 | + --sl-sidebar-item-padding-inline: 0.5rem; |
| 70 | + list-style: none; |
| 71 | + padding: 0; |
| 72 | + } |
| 73 | + |
| 74 | + li { |
| 75 | + overflow-wrap: anywhere; |
| 76 | + } |
| 77 | + |
| 78 | + ul ul li { |
| 79 | + margin-inline-start: var(--sl-sidebar-item-padding-inline); |
| 80 | + border-inline-start: 1px solid var(--sl-color-hairline-light); |
| 81 | + padding-inline-start: var(--sl-sidebar-item-padding-inline); |
| 82 | + } |
| 83 | + |
| 84 | + .large { |
| 85 | + font-size: var(--sl-text-lg); |
| 86 | + font-weight: 600; |
| 87 | + color: var(--sl-color-white); |
| 88 | + } |
| 89 | + |
| 90 | + .top-level > li + li { |
| 91 | + margin-top: 0.75rem; |
| 92 | + } |
| 93 | + |
| 94 | + summary { |
| 95 | + display: flex; |
| 96 | + align-items: center; |
| 97 | + justify-content: space-between; |
| 98 | + padding: 0.2em var(--sl-sidebar-item-padding-inline); |
| 99 | + line-height: 1.4; |
| 100 | + cursor: pointer; |
| 101 | + user-select: none; |
| 102 | + } |
| 103 | + summary::marker, |
| 104 | + summary::-webkit-details-marker { |
| 105 | + display: none; |
| 106 | + } |
| 107 | + |
| 108 | + .caret { |
| 109 | + transition: transform 0.2s ease-in-out; |
| 110 | + flex-shrink: 0; |
| 111 | + } |
| 112 | + :global([dir='rtl']) .caret { |
| 113 | + transform: rotateZ(180deg); |
| 114 | + } |
| 115 | + [open] > summary .caret { |
| 116 | + transform: rotateZ(90deg); |
| 117 | + } |
| 118 | + |
| 119 | + a { |
| 120 | + display: flex; |
| 121 | + align-items: center; |
| 122 | + border-radius: 0.25rem; |
| 123 | + text-decoration: none; |
| 124 | + color: var(--sl-color-gray-2); |
| 125 | + padding: 0.3em var(--sl-sidebar-item-padding-inline); |
| 126 | + line-height: 1.4; |
| 127 | + gap: 0.35rem; |
| 128 | + } |
| 129 | + |
| 130 | + a:hover, |
| 131 | + a:focus { |
| 132 | + color: var(--sl-color-white); |
| 133 | + } |
| 134 | + |
| 135 | + [aria-current='page'], |
| 136 | + [aria-current='page']:hover, |
| 137 | + [aria-current='page']:focus { |
| 138 | + font-weight: 600; |
| 139 | + color: var(--sl-color-text-invert); |
| 140 | + background-color: var(--sl-color-text-accent); |
| 141 | + } |
| 142 | + |
| 143 | + .group-label { |
| 144 | + display: inline-flex; |
| 145 | + align-items: center; |
| 146 | + gap: 0.25rem; |
| 147 | + } |
| 148 | + |
| 149 | + .nav-external-icon { |
| 150 | + flex-shrink: 0; |
| 151 | + color: inherit; |
| 152 | + } |
| 153 | + |
| 154 | + @media (min-width: 50rem) { |
| 155 | + .top-level > li + li { |
| 156 | + margin-top: 0.5rem; |
| 157 | + } |
| 158 | + .large { |
| 159 | + font-size: var(--sl-text-base); |
| 160 | + } |
| 161 | + a { |
| 162 | + font-size: var(--sl-text-sm); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +</style> |
| 167 | + |
0 commit comments