Skip to content

Commit

Permalink
feat: add search function (#900)
Browse files Browse the repository at this point in the history
* update other files

* Child select col elements

* remove commented text

* fix button alignment

* add banner documentation

* add autocomplete search to header

* add search feature to ds header

* fix component urls

* add tag matching

* add page tags for search

* add button alignment text

* center widget buttons

* add flat polyfill
  • Loading branch information
houbly committed Dec 2, 2021
1 parent cc39230 commit d7a6346
Show file tree
Hide file tree
Showing 16 changed files with 45,308 additions and 580 deletions.
45,573 changes: 45,055 additions & 518 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
},
"dependencies": {
"@sentry/browser": "^6.2.5",
"array-flat-polyfill": "^1.0.1",
"axios": "^0.21.1",
"findandreplacedomtext": "^0.4.6",
"gulp-sass": "^4.1.0",
Expand Down
5 changes: 4 additions & 1 deletion src/wmnds/components/button/_button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $btn-transition: ease-in-out background-color 0.2s, ease-in-out border 0.2s;
text-decoration: none;
cursor: pointer;
align-items: center;
justify-content: center;
justify-content: space-between;

&:hover {
background-color: get-color(cta, 30, dark);
Expand Down Expand Up @@ -289,12 +289,15 @@ $btn-transition: ease-in-out background-color 0.2s, ease-in-out border 0.2s;
}

&--favourite {
min-height: 0;
padding: 0;
border-radius: 0;
color: get-color(text);
background: none;
font-weight: normal;
line-height: 1;
cursor: pointer;
align-items: center;

.wmnds-btn__icon {
margin-right: $size-xsm;
Expand Down
4 changes: 4 additions & 0 deletions src/wmnds/components/sign-in/_sign-in.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
font-weight: 700;
text-decoration: none;

.wmnds-btn__icon {
vertical-align: top;
}

&:hover {
text-decoration: underline;
}
Expand Down
2 changes: 2 additions & 0 deletions src/wmnds/patterns/autocomplete/_autocomplete.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
{% set selectedItem = params.selectedItem if params.selectedItem else 'Selected item' %}

{# Label #}
{% if not params.hideLabel %}
{{
wmndsFormLabel({
for: id,
contentText: label,
classes: params.label.classes
})
}}
{% endif %}

{% if params.selected %}
<div
Expand Down
4 changes: 2 additions & 2 deletions src/wmnds/patterns/cookies/cookies-banner/_cookies-banner.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
<div class="wmnds-grid wmnds-grid--justify-between wmnds-cookies-banner__group-buttons">
{{
wmndsButton({
classes: "wmnds-col-1 wmnds-col-sm-1 wmnds-col-md-12-24 wmnds-cookies-banner__accept-all-cookies",
classes: "wmnds-col-1 wmnds-col-sm-1 wmnds-col-md-12-24 wmnds-cookies-banner__accept-all-cookies wmnds-text-align-center",
contentText: "Accept all cookies"
})
}}
{{
cta({
contentText: "Manage Cookies",
classes: "wmnds-btn wmnds-col-1 wmnds-col-sm-1 wmnds-col-md-12-24",
classes: "wmnds-btn wmnds-col-1 wmnds-col-sm-1 wmnds-col-md-12-24 wmnds-text-align-center",
link: "https://www.tfwm.org.uk/manage-cookies/"
})
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<label class="wmnds-col-1-3">
<input type="checkbox" class="wmnds-find-a-timetable-widget__mode-checkbox wmnds-screenreaders-only" name="mode" value="Bus" {{ isChecked }}>
<div class="wmnds-btn wmnds-btn--mode wmnds-btn--block">
<div class="wmnds-btn wmnds-btn--mode wmnds-btn--block wmnds-text-align-center">
{{
wmndsIcon({
icon: 'modes-isolated-bus',
Expand All @@ -54,7 +54,7 @@
contentText: "Train",
link: "https://legacy.wmnetwork.co.uk/timetables/#/?mode=Train",
linkTarget: "_blank",
classes:"wmnds-col-1 wmnds-m-b-sm",
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center",
iconLeft: "modes-isolated-rail"
})
}}
Expand All @@ -68,7 +68,7 @@
contentText: "Tram",
link: "https://legacy.wmnetwork.co.uk/timetables/#/?mode=Tram&serviceNo=MM1&limit=6",
linkTarget: "_blank",
classes:"wmnds-col-1 wmnds-m-b-sm",
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center",
iconLeft: "modes-isolated-metro"
})
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
type: "mode",
contentText: "Bus",
isActive: true if mode === 'bus',
classes:"wmnds-col-1 wmnds-m-b-sm",
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center",
iconLeft: "modes-isolated-bus"
})
}}
Expand All @@ -58,7 +58,7 @@
type: "mode",
contentText: "Train",
isActive: true if mode === 'train',
classes:"wmnds-col-1 wmnds-m-b-sm",
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center",
iconLeft: "modes-isolated-rail"
})
}}
Expand All @@ -71,7 +71,7 @@
type: "mode",
contentText: "Tram",
link: "#/",
classes:"wmnds-col-1 wmnds-m-b-sm",
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center",
iconLeft: "modes-isolated-metro"
})
}}
Expand Down
15 changes: 14 additions & 1 deletion src/wmnds/patterns/header-v2/_header-v2.njk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% from "wmnds/components/breadcrumb/_breadcrumb.njk" import wmndsBreadcrumb %}
{% from "wmnds/patterns/cookies/cookies-banner/_cookies-banner.njk" import wmndsCookiesBanner %}
{% from "wmnds/patterns/search/search-bar/_search-bar.njk" import wmndsSearchbar %}
{% from "wmnds/patterns/autocomplete/_autocomplete.njk" import wmndsAutocomplete %}

<!-- Skip to content link -->
{{
Expand Down Expand Up @@ -141,7 +142,19 @@
</button>
<div class="wmnds-mega-menu__container wmnds-mega-menu__search-container">
<div class="wmnds-search-container">
{{wmndsSearchbar()}}
{% if params.wmndsSearch %}
{{
wmndsAutocomplete(
params.wmndsSearch
)
}}
<div id="wmndsSearchSuggestions" class="wmnds-autocomplete-suggestions" style="display: none;">
</div>
{% else %}
{{
wmndsSearchbar(params.search)
}}
{% endif %}
{% if(params.searchSuggestions) %}
<p class="wmnds-search-heading wmnds-h1">I want to...</p>
<div class="wmnds-grid">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<h2>Plan your journey</h2>
<div class="wmnds-grid wmnds-grid--spacing-2-md">
<div class="wmnds-col-1-2">
<a class="wmnds-btn wmnds-btn--secondary wmnds-col-1" href="{{fromToLinks.from}}" referrerpolicy="origin" target="_blank" rel="noreferrer">From here</a>
<a class="wmnds-btn wmnds-btn--secondary wmnds-col-1 wmnds-text-align-center" href="{{fromToLinks.from}}" referrerpolicy="origin" target="_blank" rel="noreferrer">From here</a>
</div>
<div class="wmnds-col-1-2">
<a class="wmnds-btn wmnds-btn--secondary wmnds-col-1" href="{{fromToLinks.from}}" referrerpolicy="origin" target="_blank" rel="noreferrer">To here</a>
<a class="wmnds-btn wmnds-btn--secondary wmnds-col-1 wmnds-text-align-center" href="{{fromToLinks.from}}" referrerpolicy="origin" target="_blank" rel="noreferrer">To here</a>
</div>
</div>
</div>
Expand Down Expand Up @@ -220,7 +220,7 @@
contentText: "Bus",
type: "mode",
iconClasses: "wmnds-btn__coloured-icon--bus",
classes:"wmnds-col-1 wmnds-m-b-sm"
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center"
})
}}
</div>
Expand All @@ -232,7 +232,7 @@
contentText: "Tram",
type: "mode",
iconClasses: "wmnds-btn__coloured-icon--metro",
classes:"wmnds-col-1 wmnds-m-b-sm"
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center"
})
}}
</div>
Expand All @@ -244,7 +244,7 @@
contentText: "Train",
type: "mode",
iconClasses: "wmnds-btn__coloured-icon--train",
classes:"wmnds-col-1 wmnds-m-b-sm"
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center"
})
}}
</div>
Expand All @@ -258,7 +258,7 @@
contentText: "Walk",
type: "mode",
iconClasses: "wmnds-btn__coloured-icon--walk",
classes:"wmnds-col-1 wmnds-m-b-sm"
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center"
})
}}
</div>
Expand All @@ -270,7 +270,7 @@
contentText: "Cycle",
type: "mode",
iconClasses: "wmnds-btn__coloured-icon--cycle",
classes:"wmnds-col-1 wmnds-m-b-sm"
classes:"wmnds-col-1 wmnds-m-b-sm wmnds-text-align-center"
})
}}
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/wmnds/patterns/search/search-bar/_search-bar.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{% macro wmndsSearchbar(params) %}

<form class="wmnds-search-bar">
<input aria-label="Search" type="text" class="wmnds-search-bar__input wmnds-fe-input" placeholder="Search for tickets, timetables, travel advice…">
{% set id = params.id if params.id else "searchBar" %}
{% set placeholder = params.placeholder if params.placeholder else "Search for tickets, timetables, travel advice…" %}

<form id="{{id}}_form" class="wmnds-search-bar">
<input id="{{id}}_input" aria-label="Search" type="text" class="wmnds-search-bar__input wmnds-fe-input" placeholder="{{placeholder}}">
<button class="wmnds-search-bar__btn" type="submit">
<svg><title>Search</title><use xlink:href="#wmnds-general-search" href="#wmnds-general-search"></use></svg>
</button>
Expand Down
109 changes: 109 additions & 0 deletions src/www/_layouts/_header-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import pageData from '../data.njk.json';

const headerSearchJS = () => {
const autoComplete = document.getElementById('wmndsMegaMenuSearch');
const autoCompleteSuggestions = document.getElementById('wmndsSearchSuggestions');
const pages = [
...pageData.navItems.map(navItem => {
if (!navItem.subnavItems) {
return navItem;
}
const childMenus = navItem.subnavItems.map(({ name, href, subnavItems, unlinked, tags }) => {
const linkedChildMenu = !unlinked ? [{ name, href, tags, parent: navItem.name }] : [];
if (!subnavItems) {
return linkedChildMenu;
}

return [
...linkedChildMenu,
subnavItems.map(grandChildItem => ({
name: grandChildItem.name,
parent: name,
grandparent: navItem.name,
tags: grandChildItem.tags,
href: grandChildItem.href
}))
];
});
return childMenus.flat();
})
]
.flat(2)
.map(menu => {
const newMenu = menu;
if (!menu.href) {
delete newMenu.href;
}
if (!menu.tags) {
delete newMenu.tags;
}
return newMenu;
});

if (autoComplete) {
autoComplete.addEventListener('keydown', e => {
if (e.key === ' ') e.target.value = `${e.target.value} `;
});
autoComplete.addEventListener('keyup', e => {
const query = e.target.value;
autoCompleteSuggestions.innerHTML = '';
if (query.length > 2) {
const matchedPages = pages.filter(page => {
let hasMatch = false;
if (
page.name.toLowerCase().includes(e.target.value.toLowerCase()) ||
(page.parent && page.parent.toLowerCase().includes(e.target.value.toLowerCase())) ||
(page.grandparent &&
page.grandparent.toLowerCase().includes(e.target.value.toLowerCase())) ||
(page.tags && page.tags.toLowerCase().includes(e.target.value.toLowerCase()))
) {
hasMatch = true;
}
return hasMatch;
});

if (matchedPages.length) {
autoCompleteSuggestions.style.display = 'block';
matchedPages.forEach(page => {
// Construct page text node
const text = document.createTextNode(page.name);
const suggestionText = document.createElement('strong');
suggestionText.classList.add('wmnds-col-1');
suggestionText.appendChild(text);

// Construct breadcrumbs node
const crumbs = document.createTextNode(
`${page.grandparent ? `${page.grandparent} > ` : ''}${
page.parent ? `${page.parent} > ` : ''
}${page.name}`
);
const suggestionCrumbs = document.createElement('span');
suggestionCrumbs.appendChild(crumbs);

const url = str => str.toLowerCase().replace(' ', '-');
const suggestionHref =
page.href ||
`/${page.grandparent ? `${url(page.grandparent)}/` : ''}${
page.parent && page.grandparent !== 'Components' ? `${url(page.parent)}/` : ''
}${url(page.name)}/`;

const suggestionLink = document.createElement('a');
suggestionLink.setAttribute('href', suggestionHref);
suggestionLink.setAttribute('tabindex', '0');
suggestionLink.classList.add('wmnds-autocomplete-suggestions__li', 'wmnds-col-1');
suggestionLink.appendChild(suggestionText);
suggestionLink.appendChild(suggestionCrumbs);

autoCompleteSuggestions.appendChild(suggestionLink);
});
} else {
autoCompleteSuggestions.style.display = 'none';
}
} else {
autoCompleteSuggestions.style.display = 'none';
}
});
}
};

export default headerSearchJS;
6 changes: 6 additions & 0 deletions src/www/_layouts/_master-layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
section: section,
pageTitle: pageTitle,
hideBreadcrumb: hideBreadcrumb
},
search: true,
wmndsSearch: {
id: "wmndsMegaMenuSearch",
placeholder: 'Search for a page, component or pattern...',
hideLabel: true
}
})
}}
Expand Down
Loading

0 comments on commit d7a6346

Please sign in to comment.