Skip to content

Commit

Permalink
Search WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlaki committed Mar 7, 2024
1 parent 2b980f7 commit aee9daa
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('dotenv').config();

const { execSync } = require('child_process');
const { JSDOM } = require('jsdom');
const { parse, stringify } = require('himalaya');
const dateFilter = require('./src/filters/date.js');
Expand Down Expand Up @@ -198,6 +199,10 @@ module.exports = config => {
</div>`;
});

config.on('eleventy.after', () => {
execSync(`npx pagefind --site dist --glob \"**/*.html\"`, { encoding: 'utf-8' })
});

if (isProduction) {
config.addTransform('htmlmin', htmlMinTransform);
}
Expand Down
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"markdown-it-anchor": "^8.6.7",
"moment": "^2.30.1",
"npm-run-all": "^4.1.5",
"pagefind": "^1.0.4",
"sass": "^1.71.1",
"slugify": "^1.6.6",
"sprucecss": "^2.3.1",
Expand Down
15 changes: 15 additions & 0 deletions src/_includes/icon/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions src/_includes/layout/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@
<link rel="stylesheet" href="/css/main.css">
</head>
<body>
<a class="btn btn--primary skip-link" href="#content">Skip to content</a>
{% include "partial/header.html" %}
<div class="site-wrapper">
<a class="btn btn--primary skip-link" href="#content">Skip to content</a>
{% include "partial/header.html" %}

<main id="content">
{% block content %}{% endblock %}
</main>
<main id="content">
{% block content %}{% endblock %}
</main>

{% include "partial/github-cta.html" %}
{% include "partial/footer.html" %}
{% include "partial/github-cta.html" %}
{% include "partial/footer.html" %}
</div>

{% include "partial/search-modal.html" %}

<script defer src="/js/navigation.js"></script>
<script defer src="/js/accordion-card.js"></script>
<script defer src="/js/theme-switcher.js"></script>
<script defer src="/js/modal.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<script defer src="/js/toc.js"></script>
<script src="/js/cookie-consent.js?{{ assetHash }}" type="module"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/_includes/layout/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h1 class="l-documentation__title">{{ title }}</h1>
{{ component.content | toc | safe }}
</div>
</div>
<div class="l-documentation__content">
<div class="l-documentation__content" data-pagefind-body>
<div class="post-content">
{{ component.content | markdown | safe }}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/_includes/layout/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% include "partial/sidebar-docs.html" %}
</aside>
<div class="l-documentation__body-helper">
<div class="l-documentation__body">
<div class="l-documentation__body" data-pagefind-body>
<div class="l-documentation__title-helper">
<h1 class="l-documentation__title">{{ title }}</h1>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/_includes/layout/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
} %}

{% block content %}
<main id="content" class="l-template">
<main id="content" class="l-template" data-pagefind-body>
<div class="container">
{% include "partial/post-heading.html" %}

Expand Down
25 changes: 14 additions & 11 deletions src/_includes/partial/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@
</div>
{% if site.socials.length %}
<div class="site-header__actions">
{% for social in site.socials %}
<a
aria-label="{{ social.name }}"
class="btn btn--primary btn--icon btn--rounded"
href="{{ social.url }}"
target="_blank"
rel="noopener noreferrer"
>
{% svgIcon social.icon, "btn__icon" %}
</a>
{% endfor %}
{% include "partial/open-search.html" %}
<div class="site-header__socials">
{% for social in site.socials %}
<a
aria-label="{{ social.name }}"
class="btn btn--primary btn--icon btn--rounded"
href="{{ social.url }}"
target="_blank"
rel="noopener noreferrer"
>
{% svgIcon social.icon, "btn__icon" %}
</a>
{% endfor %}
</div>
</div>
{% endif %}
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/_includes/partial/open-search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<span class="open-search">
{% svgIcon './src/_includes/icon/search.svg', 'open-search__icon' %}
<span class="open-search__caption">Search</span>
<button class="btn btn--outline-primary btn--sm open-search__btn" data-action="open-search">
Ctrl K
</button>
</span>
7 changes: 7 additions & 0 deletions src/_includes/partial/search-modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="modal-backdrop">
<div class="modal" role="dialog" aria-modal="true">
<div id="search" class="search"></div>
</div>
</div>

<script src="/_pagefind/pagefind-ui.js" onload="new PagefindUI({ element: '#search', showImages: false });"></script>
2 changes: 1 addition & 1 deletion src/js/cookie-consent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
setCookie, getCookie, issetCookie, removeCookie,
} from './cookie';
} from './cookie.js';

Check failure on line 3 in src/js/cookie-consent.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected use of file extension "js" for "./cookie.js"

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Mar 7, 2024

Contributor

@adamlaki This was just fixed in #14

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Mar 7, 2024

Contributor

May be related to this?

} from './cookie.js'; // eslint-disable-line

This comment has been minimized.

This comment has been minimized.

Copy link
@szepeviktor

szepeviktor Mar 7, 2024

Contributor

🤯 There is no JavaScript build process

<script defer src="/js/navigation.js"></script>
<script defer src="/js/accordion-card.js"></script>
<script defer src="/js/theme-switcher.js"></script>
<script defer src="/js/modal.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<script defer src="/js/toc.js"></script>
<script src="/js/cookie-consent.js?{{ assetHash }}" type="module"></script>
<script src="/js/scripts.js?{{ assetHash }}" type="module"></script>

This comment has been minimized.

Copy link
@adamlaki

adamlaki Mar 8, 2024

Author Member

Yeah, sorry, Viktor, I had to revert it because if you use js-module without a compiler (like a native browser), you need the extension; otherwise, it can't find the file.


(() => {
const prefix = 'spruce';
Expand Down
45 changes: 45 additions & 0 deletions src/js/modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(() => {
let activeElement = null;
const siteWrapper = document.querySelector('.site-wrapper');
const button = document.querySelector('[data-action="open-search"]');
const input = document.querySelector('.pagefind-ui__search-input');
const modal = document.querySelector('.modal-backdrop');

if (!button || !modal) return;

function openModal() {
activeElement = document.activeElement;
siteWrapper.setAttribute('inert', '');
modal.classList.add('modal-backdrop--open');
input.focus();
}

function closeModal() {
siteWrapper.removeAttribute('inert');
modal.classList.remove('modal-backdrop--open');
activeElement.focus();
}

function handleKeyDown(e) {
if (e.code === 'Escape') {
closeModal();
}

if (e.ctrlKey && e.code === 'KeyK') {
e.preventDefault();
openModal();
}
}

modal.addEventListener('click', (e) => {
if (e.target === e.currentTarget) {
closeModal();
}
});

button.addEventListener('click', () => {
openModal();
});

window.addEventListener('keydown', handleKeyDown);
})();
3 changes: 3 additions & 0 deletions src/scss/component/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
@forward 'text-btn';
@forward 'form';
@forward 'pagination';
@forward 'pagefind';
@forward 'modal';
@forward 'template';
@forward 'notification';
@forward 'open-search';
@forward 'tags';
@forward 'ide';
@forward 'code-tab';
Expand Down
24 changes: 24 additions & 0 deletions src/scss/component/_modal.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@use 'sprucecss/scss/spruce' as *;

.modal-backdrop {
backdrop-filter: blur(0.35rem);
background-color: hsl(0deg 0% 100% / 35%);
display: none;
inset: 0;
padding: spacer('s');
position: fixed;
z-index: 100;

&--open {
display: block;
}
}

.modal {
background-color: color('background');
border-radius: config('border-radius-sm', $display);
box-shadow: get-css-variable(--box-shadow);
margin-block: 7rem;
margin-inline: auto;
max-inline-size: 40rem;
}
15 changes: 15 additions & 0 deletions src/scss/component/_open-search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@use 'sprucecss/scss/spruce' as *;

.open-search {
@include a11y-card-link('.open-search__btn', true);
align-items: center;
display: flex;
gap: spacer('xs');

&__icon {
--size: 1rem;
block-size: var(--size);
color: color('icon', 'search');
inline-size: var(--size);
}
}
Loading

0 comments on commit aee9daa

Please sign in to comment.