Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(widget): adds option to use inherited font in the widget #295

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/pages/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const initializeCode = html`
const widget = window.PlannerWeb.createWidget({
urlBase: 'https://reiseplanlegger.example.no/',
language: 'nn', // supports 'nb', 'nn' and 'en'

// Optional options
outputOverrideOptions: {
// Inherit font from page website.
// By default it uses Roboto as the hosted planner web solution.
inheritFont: false,
},
});

// After loading JS and CSS file it can be initialized
Expand Down Expand Up @@ -171,7 +178,10 @@ function WidgetContent({
<h2>Demo</h2>
<div dangerouslySetInnerHTML={{ __html: html }} />

<p>Note: Widget is without padding. Should be up to consumer to decide when integrating.</p>
<p>
Note: Widget is without padding. Should be up to consumer to decide when
integrating.
</p>

<h2>Installation (latest version v{data.latest.version})</h2>

Expand All @@ -184,6 +194,22 @@ function WidgetContent({
<h3>HTML output</h3>
<CopyMarkupLarge content={html} />

<div>
<p style={{ marginTop: '2rem' }}>
<strong>Note</strong>: If using output override options when calling{' '}
<code>createWidget</code> and copy-paste the HTML code you would have
to add classes to the container (element with the class{' '}
<code>widget-module__wrapper</code>) manually.
</p>

<ul style={{ listStylePosition: 'inside', marginTop: '1rem' }}>
<li>
<code>.widget-inheritFont</code>: Inherit font family from the
website
</li>
</ul>
</div>

<h3>Scripts (UMD / ESM)</h3>

<CopyMarkup content={scripts(lib.urls?.URL_JS_UMD)} />
Expand Down
7 changes: 7 additions & 0 deletions src/widget/widget.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,10 @@
.messageBox[hidden] {
display: none;
}
/**
* Configurable options for widget
*/
.inheritFont,
.inheritFont * {
font-family: inherit !important;
}
26 changes: 23 additions & 3 deletions src/widget/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ function createSettingsConstants(urlBase: string) {
};
}

type OutputOverrideOptions = {
inheritFont?: boolean;
};
export type WidgetOptions = {
urlBase: string;
language?: Languages;
outputOverrideOptions?: Partial<OutputOverrideOptions>;
};
export type PlannerWebOutput = {
output: string;
Expand All @@ -51,10 +55,18 @@ export type PlannerWebOutput = {
export function createWidget({
urlBase,
language = 'en',
outputOverrideOptions = {},
}: WidgetOptions): PlannerWebOutput {
const texts = translations(language);
const settings = createSettingsConstants(urlBase);
const output = createOutput(settings, texts);

const defaultOutputOverrideOptions: OutputOverrideOptions = {
inheritFont: false,
...outputOverrideOptions,
};

const output = createOutput(settings, texts, defaultOutputOverrideOptions);

return {
output,
init,
Expand Down Expand Up @@ -225,7 +237,11 @@ class MessageBox extends HTMLElement {
}
}

function createOutput({ URL_BASE }: SettingConstants, texts: Texts) {
function createOutput(
{ URL_BASE }: SettingConstants,
texts: Texts,
outputOverrideOptions: OutputOverrideOptions,
) {
function searchItem(item: GeocoderFeature) {
const img = venueIcon(item);
const title = el('span', [item.name]);
Expand Down Expand Up @@ -704,7 +720,11 @@ function createOutput({ URL_BASE }: SettingConstants, texts: Texts) {
`;

const output = html`
<div class="${style.wrapper} ${style.lightWrapper}">
<div
class="${style.wrapper} ${style.lightWrapper} ${outputOverrideOptions.inheritFont
? style.inheritFont
: ''}"
>
<nav class="${style.nav}">
<ul class="${style.tabs} js-tablist">
<li>
Expand Down
Loading