Skip to content

Commit

Permalink
Style search components to match designs (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored May 1, 2023
1 parent 436d4f6 commit e5a2e09
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 37 deletions.
8 changes: 2 additions & 6 deletions packages/starbook/components/ContentPanel.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<style>
.content-panel {
padding: 1.5rem 1rem;
padding: 1.5rem var(--sb-content-pad-x);
}
.content-panel + .content-panel {
border-top: 1px solid var(--sb-color-hairline);
Expand All @@ -17,11 +17,7 @@
margin-top: 1.5rem;
}

@media (min-width: 50em) {
.content-panel {
padding: 1.5rem;
}

@media (min-width: 72rem) {
.container {
margin-inline-start: auto;
}
Expand Down
25 changes: 23 additions & 2 deletions packages/starbook/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Props {
<div class="header">
<a href={import.meta.env.BASE_URL} class="site-title">{config.title}</a>
<Search />
<div class="hidden md:visible right-group">
<div class="hidden md:flex right-group">
<ThemeSelect />
<LanguageSelect locale={Astro.props.locale} />
</div>
Expand All @@ -21,6 +21,7 @@ interface Props {
<style>
.header {
display: flex;
gap: var(--sb-nav-gap);
justify-content: space-between;
align-items: center;
height: 100%;
Expand All @@ -35,7 +36,27 @@ interface Props {
}

.right-group {
display: flex;
gap: 1rem;
}

@media (min-width: 50rem) {
.header {
--__sidebar-width: var(--sb-sidebar-width) - var(--sb-nav-pad-x);
--__main-column-fr: calc(
(100% - var(--sb-content-width) - 2 * var(--sb-sidebar-width)) / 2
);
display: grid;
grid-template-columns:
/* 1 (site title): runs up until the main content column’s left edge. */
calc(
var(--__sidebar-width) +
max(0rem, var(--__main-column-fr) - var(--sb-nav-gap))
)
/* 2 (search box): all free space that is available. */
1fr
/* 3 (right items): use the space that these need. */
auto;
align-content: center;
}
}
</style>
4 changes: 4 additions & 0 deletions packages/starbook/components/Icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const Icons = {
'<path d="M12 11C11.7348 11 11.4804 11.1054 11.2929 11.2929C11.1054 11.4804 11 11.7348 11 12V16C11 16.2652 11.1054 16.5196 11.2929 16.7071C11.4804 16.8946 11.7348 17 12 17C12.2652 17 12.5196 16.8946 12.7071 16.7071C12.8946 16.5196 13 16.2652 13 16V12C13 11.7348 12.8946 11.4804 12.7071 11.2929C12.5196 11.1054 12.2652 11 12 11ZM12.38 7.08C12.1365 6.97998 11.8635 6.97998 11.62 7.08C11.4973 7.12759 11.3851 7.19896 11.29 7.29C11.2017 7.3872 11.1306 7.49882 11.08 7.62C11.024 7.73868 10.9966 7.86882 11 8C10.9992 8.13161 11.0245 8.26207 11.0742 8.38391C11.124 8.50574 11.1973 8.61656 11.29 8.71C11.3872 8.79833 11.4988 8.86936 11.62 8.92C11.7715 8.98224 11.936 9.00632 12.099 8.99011C12.2619 8.97391 12.4184 8.91792 12.5547 8.82707C12.691 8.73622 12.8029 8.61328 12.8805 8.46907C12.9582 8.32486 12.9992 8.16378 13 8C12.9963 7.73523 12.8927 7.48163 12.71 7.29C12.6149 7.19896 12.5028 7.12759 12.38 7.08ZM12 2C10.0222 2 8.08879 2.58649 6.4443 3.6853C4.79981 4.78412 3.51809 6.3459 2.76121 8.17317C2.00433 10.0004 1.8063 12.0111 2.19215 13.9509C2.578 15.8907 3.53041 17.6725 4.92894 19.0711C6.32746 20.4696 8.10929 21.422 10.0491 21.8079C11.9889 22.1937 13.9996 21.9957 15.8268 21.2388C17.6541 20.4819 19.2159 19.2002 20.3147 17.5557C21.4135 15.9112 22 13.9778 22 12C22 10.6868 21.7413 9.38642 21.2388 8.17317C20.7363 6.95991 19.9997 5.85752 19.0711 4.92893C18.1425 4.00035 17.0401 3.26375 15.8268 2.7612C14.6136 2.25866 13.3132 2 12 2ZM12 20C10.4178 20 8.87104 19.5308 7.55544 18.6518C6.23985 17.7727 5.21447 16.5233 4.60897 15.0615C4.00347 13.5997 3.84504 11.9911 4.15372 10.4393C4.4624 8.88743 5.22433 7.46197 6.34315 6.34315C7.46197 5.22433 8.88743 4.4624 10.4393 4.15372C11.9911 3.84504 13.5997 4.00346 15.0615 4.60896C16.5233 5.21447 17.7727 6.23984 18.6518 7.55544C19.5308 8.87103 20 10.4177 20 12C20 14.1217 19.1572 16.1566 17.6569 17.6569C16.1566 19.1571 14.1217 20 12 20Z"></path>',
magnifier:
'<path d="M21.71 20.29L18 16.61C19.4401 14.8144 20.1375 12.5353 19.9488 10.2413C19.7601 7.94733 18.6997 5.81281 16.9855 4.27667C15.2714 2.74053 13.0338 1.91954 10.7329 1.9825C8.43203 2.04546 6.24272 2.98759 4.61514 4.61517C2.98756 6.24275 2.04543 8.43207 1.98247 10.7329C1.91951 13.0338 2.7405 15.2714 4.27664 16.9855C5.81278 18.6997 7.9473 19.7601 10.2413 19.9488C12.5353 20.1375 14.8144 19.4401 16.61 18L20.29 21.68C20.383 21.7738 20.4936 21.8482 20.6154 21.8989C20.7373 21.9497 20.868 21.9758 21 21.9758C21.132 21.9758 21.2627 21.9497 21.3846 21.8989C21.5064 21.8482 21.617 21.7738 21.71 21.68C21.8902 21.4936 21.991 21.2444 21.991 20.985C21.991 20.7257 21.8902 20.4765 21.71 20.29ZM11 18C9.61553 18 8.26215 17.5895 7.111 16.8203C5.95986 16.0511 5.06265 14.9579 4.53284 13.6788C4.00303 12.3997 3.8644 10.9923 4.1345 9.63439C4.4046 8.27653 5.07128 7.02925 6.05025 6.05028C7.02922 5.07131 8.2765 4.40463 9.63436 4.13453C10.9922 3.86443 12.3997 4.00306 13.6788 4.53287C14.9579 5.06268 16.0511 5.95989 16.8203 7.11103C17.5895 8.26218 18 9.61556 18 11C18 12.8565 17.2625 14.637 15.9497 15.9498C14.637 17.2625 12.8565 18 11 18Z"></path>',
'forward-slash':
'<path d="M17 2H7C5.67392 2 4.40215 2.52678 3.46447 3.46447C2.52678 4.40215 2 5.67392 2 7V17C2 18.3261 2.52678 19.5979 3.46447 20.5355C4.40215 21.4732 5.67392 22 7 22H17C18.3261 22 19.5979 21.4732 20.5355 20.5355C21.4732 19.5979 22 18.3261 22 17V7C22 5.67392 21.4732 4.40215 20.5355 3.46447C19.5979 2.52678 18.3261 2 17 2ZM20 17C20 17.7956 19.6839 18.5587 19.1213 19.1213C18.5587 19.6839 17.7956 20 17 20H7C6.20435 20 5.44129 19.6839 4.87868 19.1213C4.31607 18.5587 4 17.7956 4 17V7C4 6.20435 4.31607 5.44129 4.87868 4.87868C5.44129 4.31607 6.20435 4 7 4H17C17.7956 4 18.5587 4.31607 19.1213 4.87868C19.6839 5.44129 20 6.20435 20 7V17Z" /><path d="M15.2929 6.70711C15.6834 6.31658 16.3166 6.31658 16.7071 6.70711C17.0976 7.09763 17.0976 7.7308 16.7071 8.12132L8.22183 16.6066C7.8313 16.9971 7.19814 16.9971 6.80761 16.6066C6.41709 16.2161 6.41709 15.5829 6.80761 15.1924L15.2929 6.70711Z" />',
close:
'<path d="M13.41 11.9999L19.71 5.70994C19.8983 5.52164 20.0041 5.26624 20.0041 4.99994C20.0041 4.73364 19.8983 4.47825 19.71 4.28994C19.5217 4.10164 19.2663 3.99585 19 3.99585C18.7337 3.99585 18.4783 4.10164 18.29 4.28994L12 10.5899L5.71 4.28994C5.5217 4.10164 5.2663 3.99585 5 3.99585C4.7337 3.99585 4.4783 4.10164 4.29 4.28994C4.1017 4.47825 3.99591 4.73364 3.99591 4.99994C3.99591 5.26624 4.1017 5.52164 4.29 5.70994L10.59 11.9999L4.29 18.2899C4.19627 18.3829 4.12188 18.4935 4.07111 18.6154C4.02034 18.7372 3.9942 18.8679 3.9942 18.9999C3.9942 19.132 4.02034 19.2627 4.07111 19.3845C4.12188 19.5064 4.19627 19.617 4.29 19.7099C4.38296 19.8037 4.49356 19.8781 4.61542 19.9288C4.73728 19.9796 4.86799 20.0057 5 20.0057C5.13201 20.0057 5.26272 19.9796 5.38458 19.9288C5.50644 19.8781 5.61704 19.8037 5.71 19.7099L12 13.4099L18.29 19.7099C18.383 19.8037 18.4936 19.8781 18.6154 19.9288C18.7373 19.9796 18.868 20.0057 19 20.0057C19.132 20.0057 19.2627 19.9796 19.3846 19.9288C19.5064 19.8781 19.617 19.8037 19.71 19.7099C19.8037 19.617 19.8781 19.5064 19.9289 19.3845C19.9797 19.2627 20.0058 19.132 20.0058 18.9999C20.0058 18.8679 19.9797 18.7372 19.9289 18.6154C19.8781 18.4935 19.8037 18.3829 19.71 18.2899L13.41 11.9999Z" />',
error:
'<path d="M12 7C11.7348 7 11.4804 7.10536 11.2929 7.29289C11.1054 7.48043 11 7.73478 11 8V12C11 12.2652 11.1054 12.5196 11.2929 12.7071C11.4804 12.8946 11.7348 13 12 13C12.2652 13 12.5196 12.8946 12.7071 12.7071C12.8946 12.5196 13 12.2652 13 12V8C13 7.73478 12.8946 7.48043 12.7071 7.29289C12.5196 7.10536 12.2652 7 12 7ZM12 15C11.8022 15 11.6089 15.0586 11.4444 15.1685C11.28 15.2784 11.1518 15.4346 11.0761 15.6173C11.0004 15.8 10.9806 16.0011 11.0192 16.1951C11.0578 16.3891 11.153 16.5673 11.2929 16.7071C11.4327 16.847 11.6109 16.9422 11.8049 16.9808C11.9989 17.0194 12.2 16.9996 12.3827 16.9239C12.5654 16.8482 12.7216 16.72 12.8315 16.5556C12.9414 16.3911 13 16.1978 13 16C13 15.7348 12.8946 15.4804 12.7071 15.2929C12.5196 15.1054 12.2652 15 12 15ZM21.71 7.56L16.44 2.29C16.2484 2.10727 15.9948 2.00368 15.73 2H8.27C8.00523 2.00368 7.75163 2.10727 7.56 2.29L2.29 7.56C2.10727 7.75163 2.00368 8.00523 2 8.27V15.73C2.00368 15.9948 2.10727 16.2484 2.29 16.44L7.56 21.71C7.75163 21.8927 8.00523 21.9963 8.27 22H15.73C15.9948 21.9963 16.2484 21.8927 16.44 21.71L21.71 16.44C21.8927 16.2484 21.9963 15.9948 22 15.73V8.27C21.9963 8.00523 21.8927 7.75163 21.71 7.56ZM20 15.31L15.31 20H8.69L4 15.31V8.69L8.69 4H15.31L20 8.69V15.31Z"></path>',
warning:
Expand Down
2 changes: 1 addition & 1 deletion packages/starbook/components/RightSidebarPanel.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<style>
.right-sidebar-panel {
padding: 1rem;
padding: 1rem var(--sb-sidebar-pad-x);
}
.right-sidebar-panel + .right-sidebar-panel {
border-top: 1px solid var(--sb-color-hairline);
Expand Down
273 changes: 259 additions & 14 deletions packages/starbook/components/Search.astro
Original file line number Diff line number Diff line change
@@ -1,41 +1,286 @@
---
import '@pagefind/default-ui/css/ui.css';
import Icon from './Icon.astro';
---

<div class="search-container">
<div id="starbook__search"></div>
</div>
<site-search>
<button data-open-modal disabled>
<Icon name="magnifier" label="Search" />
<span class="hidden md:block" aria-hidden="true">Search</span>
<Icon
name="forward-slash"
class="hidden md:block"
label="(Press / to search)"
/>
</button>

<dialog style="padding:0" aria-label="Search the documentation">
<div class="dialog-frame">
<button data-close-modal class="flex md:hidden">Cancel</button>
<div class="search-container">
<div id="starbook__search"></div>
</div>
</div>
</dialog>
</site-search>

<script>
class SiteSearch extends HTMLElement {
constructor() {
super();
const openBtn = this.querySelector<HTMLButtonElement>(
'button[data-open-modal]'
)!;
const closeBtn = this.querySelector<HTMLButtonElement>(
'button[data-close-modal]'
)!;
const dialog = this.querySelector('dialog')!;
const dialogFrame = this.querySelector('.dialog-frame')!;

/** Close the modal if a user clicks outside of the modal. */
const onWindowClick = (event: MouseEvent) => {
if (!dialogFrame.contains(event.target as Node)) closeModal();
};

const openModal = (event?: MouseEvent) => {
dialog.showModal();
this.querySelector('input')?.focus();
event?.stopPropagation();
window.addEventListener('click', onWindowClick);
};

const closeModal = () => {
dialog.close();
window.removeEventListener('click', onWindowClick);
};

openBtn.addEventListener('click', openModal);
openBtn.disabled = false;
closeBtn.addEventListener('click', closeModal);

// Listen for `/` and `cmd + k` keyboard shortcuts.
window.addEventListener('keydown', (e) => {
if (e.metaKey === true && e.key === 'k') {
dialog.open ? closeModal() : openModal();
e.preventDefault();
} else if (e.key === '/' && !dialog.open) {
openModal();
e.preventDefault();
}
});
}
}
customElements.define('site-search', SiteSearch);

window.addEventListener('DOMContentLoaded', () => {
const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1));
onIdle(async () => {
const { PagefindUI } = await import('@pagefind/default-ui');
new PagefindUI({
element: '#starbook__search',
baseUrl: import.meta.env.BASE_URL,
showImages: false,
});
});
});
</script>

<style>
.search-container {
--pagefind-ui-scale: 0.65;
button[data-open-modal] {
display: flex;
align-items: center;
gap: 0.5rem;
border: 0;
background-color: transparent;
color: var(--sb-color-gray-1);
cursor: pointer;
height: 2.5rem;
font-size: var(--sb-text-xl);
}

@media (min-width: 50rem) {
button[data-open-modal] {
border: 1px solid var(--sb-color-gray-5);
border-radius: 0.5rem;
padding-inline-start: 0.75rem;
padding-inline-end: 1rem;
background-color: var(--sb-color-black);
color: var(--sb-color-gray-2);
font-size: var(--sb-text-sm);
width: 100%;
max-width: 22rem;
}
button[data-open-modal]:hover {
border-color: var(--sb-color-gray-2);
color: var(--sb-color-white);
}

button[data-open-modal] > :last-child {
margin-inline-start: auto;
}
}

dialog {
margin: 0;
background-color: var(--sb-color-gray-6);
border: 1px solid var(--sb-color-gray-5);
width: 100%;
max-width: 100%;
height: 100%;
max-height: 100%;
box-shadow: var(--sb-shadow-lg);
}
dialog[open] {
display: grid;
}

dialog::backdrop {
background-color: var(--sb-color-backdrop-overlay);
-webkit-backdrop-filter: blur(0.25rem);
backdrop-filter: blur(0.25rem);
}

.dialog-frame {
display: flex;
flex-direction: column;
gap: 1rem;
padding: 1.5rem;
}

button[data-close-modal] {
position: absolute;
z-index: 1;
display: flex;
align-items: center;
align-self: flex-end;
height: calc(64px * var(--pagefind-ui-scale));
padding: 0.25rem;
border: 0;
background: transparent;
cursor: pointer;
color: var(--sb-color-text-accent);
}

#starbook__search {
--pagefind-ui-primary: var(--sb-color-accent-light);
--pagefind-ui-text: var(--sb-color-gray-2);
--pagefind-ui-background: var(--sb-color-gray-6);
--pagefind-ui-background: var(--sb-color-black);
--pagefind-ui-border: var(--sb-color-gray-5);
--pagefind-ui-border-width: 1px;
--sb-search-cancel-space: 5rem;
}

position: relative;
width: 50%;
@media (min-width: 50rem) {
#starbook__search {
--sb-search-cancel-space: 0px;
}

dialog {
margin: 4rem auto auto;
border-radius: 0.5rem;
width: 90%;
max-width: 40rem;
height: max-content;
min-height: 15rem;
max-height: calc(100% - 8rem);
}

button[data-close-modal] {
display: none;
}
}
</style>

<style is:global>
#starbook__search .pagefind-ui__form::before {
--pagefind-ui-text: var(--sb-color-gray-1);
opacity: 1;
}

#starbook__search .pagefind-ui__search-input {
color: var(--sb-color-white);
font-weight: 400;
width: calc(100% - var(--sb-search-cancel-space));
}

#starbook__search input:focus {
--pagefind-ui-border: var(--sb-color-accent);
}

#starbook__search .pagefind-ui__search-clear {
inset-inline-end: var(--sb-search-cancel-space);
width: calc(60px * var(--pagefind-ui-scale));
padding: 0;
background-color: transparent;
overflow: hidden;
}
#starbook__search .pagefind-ui__search-clear:focus {
outline: 1px solid var(--sb-color-accent);
}
#starbook__search .pagefind-ui__search-clear::before {
content: '';
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m13.41 12 6.3-6.29a1 1 0 1 0-1.42-1.42L12 10.59l-6.29-6.3a1 1 0 0 0-1.42 1.42l6.3 6.29-6.3 6.29a1 1 0 0 0 .33 1.64 1 1 0 0 0 1.09-.22l6.29-6.3 6.29 6.3a1 1 0 0 0 1.64-.33 1 1 0 0 0-.22-1.09L13.41 12Z'/%3E%3C/svg%3E")
center / 50% no-repeat;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m13.41 12 6.3-6.29a1 1 0 1 0-1.42-1.42L12 10.59l-6.29-6.3a1 1 0 0 0-1.42 1.42l6.3 6.29-6.3 6.29a1 1 0 0 0 .33 1.64 1 1 0 0 0 1.09-.22l6.29-6.3 6.29 6.3a1 1 0 0 0 1.64-.33 1 1 0 0 0-.22-1.09L13.41 12Z'/%3E%3C/svg%3E")
center / 50% no-repeat;
background-color: var(--sb-color-text-accent);
display: block;
width: 100%;
height: 100%;
}
#search {

#starbook__search .pagefind-ui__results > * + * {
margin-top: 0.5rem;
}
#starbook__search .pagefind-ui__result {
position: relative;
border: 0;
border-radius: 0.25rem;
padding: 1rem;
background-color: var(--sb-color-black);
}

#starbook__search .pagefind-ui__result:hover,
#starbook__search .pagefind-ui__result:focus-within {
outline: 1px solid var(--sb-color-accent-high);
}

#starbook__search .pagefind-ui__result:focus-within {
background-color: var(--sb-color-accent-low);
}

#starbook__search .pagefind-ui__result-thumb,
#starbook__search .pagefind-ui__result-inner {
margin-top: 0;
}

#starbook__search .pagefind-ui__result-link {
--pagefind-ui-text: var(--sb-color-white);
font-weight: 600;
}

#starbook__search .pagefind-ui__result-link:hover {
text-decoration: none;
}

#starbook__search .pagefind-ui__result-link::after {
content: '';
position: absolute;
top: 0;
width: calc(100% - 1rem);
max-width: 22rem;
background: var(--sb-color-gray-6);
padding: 0 0.5rem 0.5rem;
inset: 0;
}

#starbook__search .pagefind-ui__result-excerpt {
font-size: var(--sb-text-body-sm);
}

#starbook__search mark {
color: var(--sb-color-white);
background-color: var(--sb-color-accent-low);
font-weight: 500;
padding: 0.1em 0.2em;
}

#starbook__search .pagefind-ui__result:focus-within mark {
text-decoration: underline;
}
</style>
Loading

0 comments on commit e5a2e09

Please sign in to comment.