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

Consolidate aside implementation & add tip type #79

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
83a0ee3
feat: add icons
ShayPunter Apr 19, 2023
b273a46
feat: consolidate aisde implementation
ShayPunter Apr 19, 2023
8b43eb9
feat: add aside loc
ShayPunter Apr 19, 2023
59d1230
feat: add package swc/html-darwin-x64
ShayPunter Apr 19, 2023
006763a
Revert "feat: add package swc/html-darwin-x64"
ShayPunter Apr 19, 2023
3a8b29d
feat: add package swc/html-darwin-x64
ShayPunter Apr 19, 2023
4b29370
chore: move elements into parent element
ShayPunter Apr 19, 2023
f1c56d2
feat: added tip implementation
ShayPunter Apr 19, 2023
e434f1d
chore: change swc/html to dev dependency
ShayPunter Apr 19, 2023
b928ef6
chore: update license year
ShayPunter Apr 19, 2023
651d97f
chore: remove un-needed comment
ShayPunter Apr 19, 2023
ad81da0
chore: updated icons to make them display
ShayPunter Apr 19, 2023
0ea4548
chore: added key_term translation
ShayPunter Apr 19, 2023
a9d908b
chore: updated icon to display
ShayPunter Apr 19, 2023
b457911
chore: add missing aside type
ShayPunter Apr 20, 2023
ad53429
chore: change padding and border to fixed values
ShayPunter Apr 20, 2023
c522fc1
chore: add default
ShayPunter Apr 20, 2023
e6a60da
chore: set extend flow as optional and remove apply utility background
ShayPunter Apr 20, 2023
5c1f9fa
chore: add tip color
ShayPunter Apr 21, 2023
afbdea1
chore: add update celebration and objective color
ShayPunter Apr 21, 2023
bb49950
chore: add info aside background color
ShayPunter Apr 25, 2023
d875a84
chore: update default locale variable name
ShayPunter May 8, 2023
a415032
chore: extracted getIcon helper function
ShayPunter May 9, 2023
3b5235e
feat: made colours configurable and made them no longer rely on web d…
ShayPunter May 9, 2023
4a5426e
chore: update to not use ternaries
ShayPunter May 9, 2023
ab0a0b8
Merge branch 'main' into 77_migrate_and_consolidate_aside_implementat…
ShayPunter May 9, 2023
a102963
chore: fix default locale error remove unneeded util classes
ShayPunter May 10, 2023
c0fca12
chore: added variable and configurable scss
ShayPunter May 10, 2023
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
121 changes: 121 additions & 0 deletions data/i18n/aside.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
note:
en: Note
es: Nota
ja: メモ
ko: 메모
pt: Observação
ru: Примечание
zh: 注意

caution:
en: Caution
es: Precaución
ja: 注意
ko: 주의
pt: Cuidado
ru: Внимание
zh: 小心

warning:
en: Warning
es: Advertencia
ja: 警告
ko: 경고
pt: Aviso
ru: Предупреждение
zh: 警告

success:
en: Success
es: Éxito
ja: 成功
ko: 성공
pt: Sucesso
ru: Успех
zh: 成功

objective:
en: Objective
es: Objetivo
ja: 目的
ko: 목적
pt: Objetivo
ru: Цель
zh: 目标

try_it:
en: Try it
es: Pruebelo
ja: お試しください
ko: 시도하기
pt: Experimente
ru: Попробуйте
zh: 请尝试

gotchas:
en: Gotchas
es: Gotchas
ja: 問題の原因
ko: 갓차
pt: Pegadinhas
ru: Обратите внимание
zh: 问题

celebration:
en: Celebration
es: Celebración
ja: お祝い
ko: 축하
pt: Celebração
ru: праздник
zh: 庆典

important:
en: Important
es: Importante
ja: 重要
ko: 중요한
pt: Importante
ru: Важный
zh: 重要的

update:
en: Update
es: Actualizar
ja: 更新
ko: 업데이트
pt: Atualizar
ru: Обновлять
zh: 更新

close:
en: Close
es: Cerrar
ja: 閉じる
ko: 닫기
pt: Fechar
ru: Закрыть
zh: 关闭


updated:
en: Updated
es: Actualizado
ja: 更新済み
ko: 업데이트됨
pt: Atualizado
ru: Обновлено
zh: 更新日期

tip:
en: tip
es: consejo
ja: 端っこ
ko: 팁
pt: dica
ru: совет
zh: 尖端

key_term:
en: 'Key Term'
es: 'Término Clave'
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/celebration.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions shortcodes/Aside/icons/code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/done.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions shortcodes/Aside/icons/highlighter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/ink-highlighter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/lightbulb-outline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions shortcodes/Aside/icons/lightbulb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions shortcodes/Aside/icons/update.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions shortcodes/Aside/icons/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 165 additions & 0 deletions shortcodes/Aside/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* global __basedir */

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const {html} = require('common-tags');
const fs = require('fs');
// We need html: true since folks embed HTML inside of {% Aside %}.
// See https://markdown-it.github.io/markdown-it/#MarkdownIt.new
const md = require('markdown-it')({html: true});
const path = require('path');
const {I18nFilter} = require('../../filters/i18n');

function getLocaleFromPath(path) {
return path ? path.split('/')[1] : defaultLocale;
}

const defaultLocale = 'en';
ShayPunter marked this conversation as resolved.
Show resolved Hide resolved

const i18n = new I18nFilter().configure({
defaultLocale,
dictPaths: [path.join(__dirname, '..', '_data', 'i18n')],
});

/**
* @this {EleventyPage}
*/
function Aside(content, type = 'note') {
const locale = getLocaleFromPath(this.page && this.page.filePathStem);
ShayPunter marked this conversation as resolved.
Show resolved Hide resolved

// CSS utility classes that vary per aside type
const utilities = {
main: '',
title: '',
icon: '',
body: '',
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the styles in a way that's not needed anymore. Basically all styles this widget needs should be in styles - they shouldn't rely on anything either web.dev or d.c.c brings or alters. See the BrowserCompat shortcode for reference.

For example you are using --color-blue-darkest, which is undefined for web.dev.

Define basic styling here and make it configurable using overwritable variables.


// These two get populated based on type
let title = '';
let icon = '';

// If an icon is required, it grabs the SVG source with fs
// because in a shortcode, we have no access to includes etc
const getIcon = () => {
ShayPunter marked this conversation as resolved.
Show resolved Hide resolved
if (!icon.length) {
return '';
}

return fs.readFileSync(
path.join(__dirname, 'icons', icon),
'utf8',
);
};

// Generate all the configurations per aside type
switch (type) {
case 'note':
default:
utilities.title = 'color-state-info-text';
utilities.main = 'bg-state-info-bg color-state-info-text';
break;

case 'caution':
utilities.title = 'color-state-bad-text';
utilities.main = 'bg-state-bad-bg color-state-bad-text';
icon = 'error.svg';
title = i18n(`i18n.aside.${type}`, locale);
break;

case 'warning':
utilities.icon = 'color-state-warn-text';
utilities.main = 'bg-state-warn-bg color-state-warn-text';
icon = 'warning.svg';
title = i18n(`i18n.aside.${type}`, locale);
break;

case 'success':
utilities.main = 'bg-state-good-bg color-state-good-text';
icon = 'done.svg';
title = i18n(`i18n.aside.${type}`, locale);
break;

case 'objective':
utilities.main = 'bg-state-good-bg color-state-good-text';
icon = 'done.svg';
title = i18n(`i18n.aside.${type}`, locale);
break;

case 'gotchas':
icon = 'lightbulb.svg';
title = i18n(`i18n.aside.gotchas`, locale);
utilities.main = 'bg-tertiary-box-bg color-tertiary-box-text';
break;

case 'important':
icon = 'lightbulb.svg';
title = i18n(`i18n.aside.important`, locale);
utilities.main = 'bg-tertiary-box-bg color-tertiary-box-text';
break;

case 'key-term':
icon = 'highlighter.svg';
title = i18n(`i18n.aside.key_term`, locale);
utilities.main = 'color-secondary-box-text bg-secondary-box-bg';
break;

case 'codelab':
icon = 'code.svg';
title = i18n(`i18n.aside.try_it`, locale);
utilities.main = 'bg-quaternary-box-bg color-quaternary-box-text';
break;

case 'celebration':
utilities.main = 'bg-state-good-bg color-state-good-text';
icon = 'celebration.svg';
title = i18n(`i18n.aside.${type}`, locale);
break;

case 'update':
icon = 'update.svg';
title = i18n(`i18n.aside.${type}`, locale);
utilities.main = 'bg-state-update-bg color-state-update-text';
break;

case 'tip':
icon = 'lightbulb.svg';
title = i18n(`i18n.aside.${type}`, locale);
utilities.main = 'bg-state-good-bg color-state-good-text';
break;
}

// Make sure that we don't insert multiple newlines when this component is
// used, as it can break the parent Markdown rendering.
// See https://github.com/GoogleChrome/web.dev/issues/7640
const renderedContent = md.renderInline(content);
const titleHTML = title.length
? `<p class="cluster ${utilities.title}">` +
`<span class="aside__icon box-block ${
utilities.icon
}">${getIcon()}</span>` +
`<strong>${title}</strong></p>`
: '';
ShayPunter marked this conversation as resolved.
Show resolved Hide resolved
const asideHTML =
`<aside class="aside aside--${type} flow ${utilities.main}">` +
titleHTML +
`<div class="${utilities.body} flow">${renderedContent}</div></aside>`;

return html`${asideHTML}`;
}

module.exports = Aside;
Loading