Skip to content

Commit

Permalink
Fix modal dialog styling (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmkhoury authored Dec 12, 2023
1 parent 81ecef3 commit 2cf5116
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
listCollection, createCollection, patchCollection, getCollection,
} from '../../scripts/collections.js';
import { populateAssetViewLeftDialog } from '../../scripts/scripts.js';
import { getSelectedAssetsFromInfiniteResultsBlock, addModalEventListeners } from '../../scripts/shared.js';
import { getSelectedAssetsFromInfiniteResultsBlock } from '../../scripts/shared.js';
import createMultiSelectedAssetsTable from '../../scripts/multi-selected-assets-table.js';
import { addDialogEventListeners } from '../../scripts/dialog-html-builder.js';

// Function to create the new collection input
function createNewCollectionInput(newCollectionRadioInputContainer) {
Expand Down Expand Up @@ -298,7 +299,7 @@ export default async function decorate(block) {
dialog.close();
});

addModalEventListeners(dialog, {
addDialogEventListeners(dialog, {
closeModalOnEscape: false,
closeModalOnOutsideClick: true,
onClose: () => {
Expand Down
4 changes: 2 additions & 2 deletions blocks/adp-asset-details-modal/adp-asset-details-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
setHashParamInWindowURL,
removeParamFromWindowURL,
closeAssetDetailsModal,
addModalEventListeners,
} from '../../scripts/shared.js';
import { addDialogEventListeners } from '../../scripts/dialog-html-builder.js';
import { authorizeURL, getAssetMetadata } from '../../scripts/polaris.js';
import {
getAssetName, getAssetMimeType, getAssetTitle,
Expand Down Expand Up @@ -276,7 +276,7 @@ export default function decorate(block) {
block.querySelector('#asset-details-close').addEventListener('click', () => {
dialog.close();
});
addModalEventListeners(dialog, {
addDialogEventListeners(dialog, {
closeModalOnEscape: true,
closeModalOnOutsideClick: true,
onClose: () => {
Expand Down
5 changes: 3 additions & 2 deletions blocks/adp-download-modal/adp-download-modal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { decorateIcons } from '../../scripts/lib-franklin.js';
import { getAvailableRenditions } from '../../scripts/renditions.js';
import { logError } from '../../scripts/scripts.js';
import { createTag, closeDialogEvent, addModalEventListeners } from '../../scripts/shared.js';
import { createTag, closeDialogEvent } from '../../scripts/shared.js';
import { addDialogEventListeners } from '../../scripts/dialog-html-builder.js';
import { addAssetToContainer } from '../../scripts/asset-panel-html-builder.js';
import { emitEvent, EventNames } from '../../scripts/events.js';
import { getBearerToken } from '../../scripts/security.js';
Expand Down Expand Up @@ -32,7 +33,7 @@ export default function decorate(block) {
</dialog>`;
decorateIcons(block);
const dialog = block.querySelector('dialog');
addModalEventListeners(dialog, {
addDialogEventListeners(dialog, {
removeDialogElementOnClose: false,
closeModalOnEscape: true,
closeModalOnOutsideClick: true,
Expand Down
5 changes: 3 additions & 2 deletions blocks/adp-share-modal/adp-share-modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { decorateIcons } from '../../scripts/lib-franklin.js';
import { logError, populateAssetViewLeftDialog } from '../../scripts/scripts.js';
import { createLinkHref, addModalEventListeners } from '../../scripts/shared.js';
import { createLinkHref } from '../../scripts/shared.js';
import { addDialogEventListeners } from '../../scripts/dialog-html-builder.js';
import { createDateInput } from '../../scripts/date-input.js';
import createMultiSelectedAssetsTable from '../../scripts/multi-selected-assets-table.js';
import { createLinkShare } from '../../scripts/link-share.js';
Expand Down Expand Up @@ -293,7 +294,7 @@ export default async function decorate(block) {

await decorateIcons(block);
const dialog = block.querySelector('dialog');
addModalEventListeners(dialog);
addDialogEventListeners(dialog);
dialog.querySelector('.action-close').addEventListener('click', () => {
closeDialog(dialog);
});
Expand Down
40 changes: 36 additions & 4 deletions contenthub/hydration/hydration.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
#upload-dialog {
width: 60%;
#hydration-upload-dialog {
display: flex;
flex-direction: column;
width: 90vw;
height: 90vh;
}

#assets-upload-container {
height: 550px;

#hydration-upload-dialog .dialog-header {
display: grid;
padding: 28px 34px 0 35px;
grid-template:
'dialog-header-left dialog-header-right' 1fr
/ 1fr 1fr;
}

#hydration-upload-dialog .dialog-header span.icon {
display: flex;
align-items: center;
}

#hydration-upload-dialog .dialog-header .dialog-header-left {
display: flex;
flex-direction: column;
align-items: flex-start;
grid-area: dialog-header-left;
font: var(--bold-font-size-l);
text-transform: initial;
}

#hydration-upload-dialog .dialog-header .dialog-header-right {
display: flex;
flex-direction: column;
align-items: flex-end;
grid-area: dialog-header-right;
}

#hydration-upload-dialog .dialog-body {
flex-grow: 1;
overflow: auto;
}
21 changes: 9 additions & 12 deletions contenthub/hydration/hydration.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { createDialogHtml, addDialogEventListeners } from '../../scripts/dialog-html-builder.js';
import { loadCSS, loadScript } from '../../scripts/lib-franklin.js';
import { getBearerToken } from '../../scripts/security.js';
import { addModalEventListeners } from '../../scripts/shared.js';

export async function openUploadDialog() {
await loadScript('https://experience-qa.adobe.com/solutions/CQ-assets-upload/static-assets/resources/assets-upload.js');
// API: https://git.corp.adobe.com/CQ/assets-upload/blob/main/packages/%40assets/upload/src/components/AllInOneUpload.tsx#L22-L30
loadCSS('/contenthub/hydration/hydration.css');

const uploadDialog = document.createElement('dialog');
uploadDialog.innerHTML = `
<h4>Upload</h4>
<div id="assets-upload-container"></div>
`;

document.body.append(uploadDialog);

uploadDialog.showModal();
const { dialog, dialogBody } = createDialogHtml('hydration-upload-dialog', {
dialogHeaderLeftContent: 'Upload',
});
document.body.append(dialog);
dialog.showModal();

const container = uploadDialog.querySelector('#assets-upload-container');
const container = dialogBody;
// eslint-disable-next-line no-undef
UploadCoordinator.renderAllInOneUpload(
container,
// TODO: Configure the MFE with proper metadata
{ env: 'QA', apiToken: await getBearerToken() },
// eslint-disable-next-line no-console
() => { console.log('rendered MFE!'); },
);

addModalEventListeners(uploadDialog, {
addDialogEventListeners(dialog, {
removeDialogElementOnClose: true,
closeModalOnEscape: true,
closeModalOnOutsideClick: true,
Expand Down
4 changes: 2 additions & 2 deletions scripts/confirm-dialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { decorateIcons } from './lib-franklin.js';
import { addModalEventListeners } from './shared.js';
import { addDialogEventListeners } from './dialog-html-builder.js';

export default async function createConfirmDialog(title, text, onConfirm, confirmButtonText, onCancel, cancelButtonText) {
const dialog = document.createElement('dialog');
Expand Down Expand Up @@ -46,7 +46,7 @@ export default async function createConfirmDialog(title, text, onConfirm, confir
dialog.close();
});

addModalEventListeners(dialog.querySelector('dialog'), {
addDialogEventListeners(dialog.querySelector('dialog'), {
removeDialogElementOnClose: true,
closeModalOnEscape: true,
});
Expand Down
119 changes: 119 additions & 0 deletions scripts/dialog-html-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import decorate from '../blocks/adp-download-modal/adp-download-modal.js';
import { emitEvent, EventNames } from './events.js';
import { decorateIcons } from './lib-franklin.js';

function addContent(element, content) {
if (typeof content === 'string' && content.trim().length > 0) {
element.innerText = content;
} else if (content instanceof HTMLElement) {
element.appendChild(content);
} else if (Array.isArray(content)) {
content.forEach((item) => {
addContent(element, item);
});
}
}

export function createDialogHtml(
dialogId,
{
dialogHeaderLeftContent,
dialogHeaderRightContent,
dialogBodyContent,
dialogFooterContent,
showCloseButton,
addEventListeners,
},
) {
// eslint-disable-next-line no-param-reassign
if (showCloseButton === undefined) showCloseButton = true;
// eslint-disable-next-line no-param-reassign
if (addEventListeners === undefined) addEventListeners = true;
const dialog = document.createElement('dialog');
dialog.id = dialogId;
dialog.classList.add('dialog-container');
dialog.innerHTML = `
<div class="dialog-header">
<div class="dialog-header-left">
</div>
<div class="dialog-header-right">
</div>
</div>
</div>
<div class="dialog-body">
</div>
<div class="dialog-footer">
</div>`;
const dialogHeaderLeft = dialog.querySelector('.dialog-header-left');
const dialogHeaderRight = dialog.querySelector('.dialog-header-right');
const dialogBody = dialog.querySelector('.dialog-body');
const dialogFooter = dialog.querySelector('.dialog-footer');
addContent(dialogHeaderLeft, dialogHeaderLeftContent);
addContent(dialogHeaderRight, dialogHeaderRightContent);
if (showCloseButton) {
// create <button class="action action-close" aria-label="Close">
const buttonElem = document.createElement('button');
buttonElem.classList.add('action', 'action-close');
buttonElem.setAttribute('aria-label', 'Close');
const closeButtonHtml = '<span class="icon icon-close"></span>';
buttonElem.innerHTML = closeButtonHtml;
buttonElem.addEventListener('click', () => {
dialog.close();
});
decorateIcons(buttonElem);
dialogHeaderRight.appendChild(buttonElem);
}
addContent(dialogBody, dialogBodyContent);
addContent(dialogFooter, dialogFooterContent);
if (addEventListeners) {
addDialogEventListeners(dialog, {
closedialogOnEscape: true,
closedialogOnOutsideClick: true,
});
}
return {
dialog,
dialogHeaderLeft,
dialogHeaderRight,
dialogBody,
dialogFooter,
};
}

let numModalsOpen = 0;

export function addDialogEventListeners(dialogElement, options = {}) {
const observer = new MutationObserver((event) => {
if (event[0].attributeName === 'open') { // dialog is opening
document.body.classList.add('no-scroll');
if (dialogElement.open) {
numModalsOpen += 1;
if (numModalsOpen === 1) {
emitEvent(dialogElement, EventNames.A_MODAL_IS_OPEN);
}
} else { // dialog is closing
document.body.classList.remove('no-scroll');
if (numModalsOpen > 0) {
numModalsOpen -= 1;
}
if (numModalsOpen === 0) {
emitEvent(dialogElement, EventNames.ALL_MODALS_CLOSED);
}
if (options.onClose) {
options.onClose();
}
if (options.removeDialogElementOnClose && dialogElement.isConnected) {
dialogElement.remove();
}
}
}
});
observer.observe(dialogElement, { attributes: true });
if (options.closeModalOnOutsideClick) {
dialogElement.addEventListener('click', (event) => {
if (event.target === dialogElement) {
dialogElement.close();
}
});
}
}
39 changes: 0 additions & 39 deletions scripts/shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { unifiedShellNavigateTo } from '../contenthub/unified-shell.js';
import { EventNames, emitEvent } from './events.js';

export function closeAssetDetailsModal(block) {
document.body.classList.remove('no-scroll');
Expand Down Expand Up @@ -27,44 +26,6 @@ export function setCSSVar(cssVariableName, configValue, shouldPrependToCommaSepa
}
}

let numModalsOpen = 0;

export function addModalEventListeners(dialogElement, options = {}) {
const observer = new MutationObserver((event) => {
if (event[0].attributeName === 'open') { // dialog is opening
document.body.classList.add('no-scroll');
if (dialogElement.open) {
numModalsOpen += 1;
if (numModalsOpen === 1) {
emitEvent(dialogElement, EventNames.A_MODAL_IS_OPEN);
}
} else { // dialog is closing
document.body.classList.remove('no-scroll');
if (numModalsOpen > 0) {
numModalsOpen -= 1;
}
if (numModalsOpen === 0) {
emitEvent(dialogElement, EventNames.ALL_MODALS_CLOSED);
}
if (options.onClose) {
options.onClose();
}
if (options.removeDialogElementOnClose && dialogElement.isConnected) {
dialogElement.remove();
}
}
}
});
observer.observe(dialogElement, { attributes: true });
if (options.closeModalOnOutsideClick) {
dialogElement.addEventListener('click', (event) => {
if (event.target === dialogElement) {
dialogElement.close();
}
});
}
}

/**
* Removes a given parameter from the current URL's query string and hash parameter string.
* The window's history will be modified in place.
Expand Down
14 changes: 10 additions & 4 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,7 @@ START: Common blocks/*-infinite-results block styling
padding-top: var(--main-container-padding-top);
display: grid;
grid-template-columns: min-content max-content max-content auto min-content;
<<<<<<< HEAD
grid-template-rows: minmax(46px, auto) auto;
=======
grid-template-rows: 46px auto;
>>>>>>> ad660ce (ASSETS-31831 - Update style for content hub)
/* stylelint-disable-next-line declaration-block-no-redundant-longhand-properties */
grid-template-areas:
"refinements selection-items stats current-refinements asset-details"
Expand Down Expand Up @@ -1200,3 +1196,13 @@ END: blocks/adp-collection-header and blocks/adp-stats
padding-bottom: 35px;
justify-content: end;
}

/**
START: dialog css - see scripts/dialog-html-builder.js
*/
.dialog-container {
border-radius: 6px;
border: none;
padding: 0;
}

0 comments on commit 2cf5116

Please sign in to comment.