Skip to content

Commit

Permalink
[MWPW-158927] Countdown Timer feedbacks incorporated (adobecom#2955)
Browse files Browse the repository at this point in the history
* Fix RTL issues with Countdown Timer

* UT fix

* review comment

* review comment

* review comment

* font fixes

* do not reverse

* Revert "UT fix"

This reverts commit c7e4d1d.

* added CDT in notifications standard variant

* ends in vertical positioning fix

* review comments

* UT added for cdt in notification

* lint

* css fixes

* minor fix

* lint

* css

* update timer for instant query param

* narcis review feedback incorporated from PR adobecom#2928

* minor refactoring

* fix CDT alignment in media and modal block

---------

Co-authored-by: Rohit Sahu <[email protected]>
  • Loading branch information
2 people authored and Jens Singler committed Oct 7, 2024
1 parent a859020 commit 1dba100
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 29 deletions.
3 changes: 2 additions & 1 deletion libs/blocks/media/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default async function init(el) {
decorateTextOverrides(el);

if (el.classList.contains('countdown-timer')) {
await loadCDT(container, el.classList);
const textBlock = container.querySelector('.text');
if (textBlock) await loadCDT(textBlock, el.classList);
}
}
5 changes: 4 additions & 1 deletion libs/blocks/notification/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Notification - v1.2
*/

import { decorateBlockText, decorateBlockBg, decorateTextOverrides, decorateMultiViewport } from '../../utils/decorate.js';
import { decorateBlockText, decorateBlockBg, decorateTextOverrides, decorateMultiViewport, loadCDT } from '../../utils/decorate.js';
import { createTag, getConfig, loadStyle } from '../../utils/utils.js';

const { miloLibs, codeRoot } = getConfig();
Expand Down Expand Up @@ -135,6 +135,9 @@ async function decorateLockup(lockupArea, el) {
async function decorateForegroundText(el, container) {
const text = container?.querySelector('h1, h2, h3, h4, h5, h6, p')?.closest('div');
text?.classList.add('text');
if (el.classList.contains('countdown-timer') && !el.classList.contains('pill') && !el.classList.contains('ribbon')) {
await loadCDT(text, el.classList);
}
const iconArea = text?.querySelector('p:has(picture)');
iconArea?.classList.add('icon-area');
if (iconArea?.textContent.trim()) await decorateLockup(iconArea, el);
Expand Down
34 changes: 18 additions & 16 deletions libs/features/cdt/cdt.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
.timer-label {
font-size: var(--type-body-s-size);
font-weight: 700;
height: 27px;
}

.light .timer-label {
line-height: var(--type-body-xxl-size);
color: #000;
}

Expand All @@ -28,15 +25,20 @@
}

.horizontal .timer-label {
margin: 0 2px 27px;
margin: 0 2px var(--type-body-xxl-size);
}

.timer-block {
display: flex;
.timer-separator {
margin: 0px 2px;
line-height: var(--type-body-xxl-size);
}

.horizontal .timer-block {
margin-left: 10px;
margin-inline-start: 10px;
}

.timer-block {
display: flex;
}

.timer-fragment {
Expand All @@ -46,15 +48,13 @@
}

.timer-box {
padding: 0 9px;
width: 10px;
padding: 0 calc(var(--font-size-multiplier, 1) * 9px);
width: var(--type-detail-s-size);
border-radius: 5px;
font-size: var(--type-body-m-size);
font-weight: 700;
text-align: center;
}

.light .timer-box {
line-height: var(--type-body-xxl-size);
background-color: #222;
color: #FFF;
}
Expand All @@ -64,6 +64,10 @@
color: #1D1D1D;
}

html[dir="rtl"] .timer-unit-container {
flex-direction: row-reverse;
}

.timer-unit-container {
display: flex;
column-gap: 2px;
Expand All @@ -75,9 +79,7 @@
font-size: var(--type-body-xs-size);
font-weight: 400;
text-align: start;
}

.light .timer-unit-label {
line-height: var(--type-body-xxl-size);
color: #464646;
}

Expand Down
12 changes: 7 additions & 5 deletions libs/features/cdt/cdt.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function loadCountdownTimer(
) {
let isVisible = false;
let interval;
const oneMinuteinMs = 60000;

const instant = new URL(window.location.href)?.searchParams?.get('instant');
let currentTime = instant ? Date.parse(instant) : Date.now();

function appendTimerBox(parent, value, label) {
const fragment = createTag('div', { class: 'timer-fragment' }, null, { parent });
Expand Down Expand Up @@ -52,9 +56,6 @@ function loadCountdownTimer(
}

function updateCountdown() {
const instant = new URL(window.location.href)?.searchParams?.get('instant');
const currentTime = instant ? new Date(instant) : Date.now();

for (let i = 0; i < timeRangesEpoch.length; i += 2) {
const startTime = timeRangesEpoch[i];
const endTime = timeRangesEpoch[i + 1];
Expand All @@ -66,6 +67,7 @@ function loadCountdownTimer(
const hoursLeft = Math.floor((diffTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutesLeft = Math.floor((diffTime % (1000 * 60 * 60)) / (1000 * 60));
render(daysLeft, hoursLeft, minutesLeft);
currentTime += oneMinuteinMs;
return;
}
}
Expand All @@ -76,7 +78,6 @@ function loadCountdownTimer(
}

function startCountdown() {
const oneMinuteinMs = 60000;
updateCountdown();
interval = setInterval(updateCountdown, oneMinuteinMs);
}
Expand Down Expand Up @@ -108,13 +109,14 @@ export default async function initCDT(el, classList) {
const parsedTime = Date.parse(time?.trim());
return Number.isNaN(parsedTime) ? null : parsedTime;
});

if (timeRangesEpoch.includes(null)) {
throw new Error('Invalid format for countdown timer range');
}

const cdtDiv = createTag('div', { class: 'countdown-timer' }, null, { parent: el });
cdtDiv.classList.add(isMobile() ? 'vertical' : 'horizontal');
cdtDiv.classList.add(classList.contains('dark') ? 'dark' : 'light');
if (classList.contains('dark')) cdtDiv.classList.add('dark');
if (classList.contains('center')) cdtDiv.classList.add('center');

loadCountdownTimer(cdtDiv, cdtLabel, cdtDays, cdtHours, cdtMins, timeRangesEpoch);
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,6 @@ export async function loadCDT(el, classList) {
.then(({ default: initCDT }) => initCDT(el, classList)),
]);
} catch (error) {
window.lana?.log(`Failed to load countdown timer module: ${error}`, { tags: 'countdown-timer' });
window.lana?.log(`WARN: Failed to load countdown timer: ${error}`, { tags: 'errorType=warn,module=countdown-timer' });
}
}
46 changes: 45 additions & 1 deletion test/blocks/notification/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,49 @@ <h3 id="desktop-content"><strong>Desktop content</strong></h3>
</div>
</div>
</div>

<div>
<h3 id="banner-notifications-default">Banner Notifications (default) with Countdown countdown-timer</h3>
<p>notification</p>
<div class="notification countdown-timer">
<div>
<div>
<picture>
<source media="(max-width: 400px)" srcset="./">
<img alt="mock" loading="lazy" src="">
</picture>
</div>
<div>
<picture>
<source media="(max-width: 400px)" srcset="./">
<img alt="mock" loading="lazy" src="">
</picture>
</div>
<div>
<picture>
<source media="(max-width: 400px)" srcset="./">
<img alt="mock" loading="lazy" src="">
</picture>
</div>
</div>
<div>
<div>
<h3 id="heading-m-2430-lorem-ipsum-dolor-sit-amet-consetetur-sadis-elitr">Heading M 24/30 Lorem ipsum dolor sit amet, consetetur sadis elitr.</h3>
<p>Body M 18/27 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eius. <a href="https://usc-word-edit.officeapps.live.com/we/wordeditorframe.aspx?ui=en%2DUS&#x26;rs=en%2DUS&#x26;wopisrc=https%3A%2F%2Fadobe.sharepoint.com%2Fsites%2Fadobecom%2F_vti_bin%2Fwopi.ashx%2Ffiles%2F3de33d38f15646ef97bcba87d26c1aeb&#x26;wdenableroaming=1&#x26;mscc=1&#x26;hid=0CDF68A0-90AA-D000-70F9-F912044D2A2F&#x26;wdorigin=Other&#x26;jsapi=1&#x26;jsapiver=v1&#x26;newsession=1&#x26;corrid=7bf09044-0a00-45c1-8cdd-5ab811dd1859&#x26;usid=7bf09044-0a00-45c1-8cdd-5ab811dd1859&#x26;sftc=1&#x26;cac=1&#x26;mtf=1&#x26;sfp=1&#x26;instantedit=1&#x26;wopicomplete=1&#x26;wdredirectionreason=Unified_SingleFlush&#x26;rct=Medium&#x26;ctp=LeastProtected">See terms</a>.</p>
<p><em><a href="https://www.adobe.com/">Call to action</a></em> <strong><a href="https://www.adobe.com/">Call to action</a></strong></p>
</div>
<div>
<picture>
<source media="(max-width: 400px)" srcset="./">
<img alt="mock" loading="lazy" src="">
</picture>
</div>
</div>
</div>
<div class="section-metadata">
<div>
<div data-valign="middle">style</div>
<div data-valign="middle"><strong>l spacing</strong></div>
</div>
</div>
</div>
<div></div>
14 changes: 10 additions & 4 deletions test/blocks/notification/notification.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import { setConfig } from '../../../libs/utils/utils.js';
import { setConfig, getConfig } from '../../../libs/utils/utils.js';

const mockBody = await readFile({ path: './mocks/body.html' });
const { default: init } = await import('../../../libs/blocks/notification/notification.js');

const locales = { '': { ietf: 'en-US', tk: 'hah7vzn.css' } };
const conf = { locales };
const conf = { locales, miloLibs: 'http://localhost:2000/libs' };
setConfig(conf);
getConfig().locale.contentRoot = '/test/blocks/notification/mocks';

const mockBody = await readFile({ path: './mocks/body.html' });
const { default: init } = await import('../../../libs/blocks/notification/notification.js');
document.head.innerHTML = '<link rel="stylesheet" href="../../../libs/blocks/notification/notification.css"><meta name="countdown-timer" content="2024-08-26 12:00:00 PST,2026-08-30 00:00:00 PST">';

describe('notification', async () => {
let notifs;
Expand Down Expand Up @@ -45,6 +48,9 @@ describe('notification', async () => {
const border = notifs[2].querySelector(':scope > .border');
expect(border).to.exist;
});
it('has a cdt', () => {
expect(notifs[15].querySelectorAll('.timer-label')).to.have.lengthOf(1);
});
});

describe('ribbon notifications', () => {
Expand Down

0 comments on commit 1dba100

Please sign in to comment.