Skip to content

Commit

Permalink
fix: require ctx-name attribute for all the public blocks and wait …
Browse files Browse the repository at this point in the history
…for it with 300ms timeout

this is required mostly for Angular projects, because it sets binded attributes async
  • Loading branch information
nd0ut committed Oct 10, 2023
1 parent 9590f4e commit 7150f4c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 12 deletions.
19 changes: 18 additions & 1 deletion abstract/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { sharedConfigKey } from './sharedConfigKey.js';
import { toKebabCase } from '../utils/toKebabCase.js';
import { warnOnce } from '../utils/warnOnce.js';
import { getPluralForm } from '../utils/getPluralForm.js';
import { waitForAttribute } from '../utils/waitForAttribute.js';

const TAG_PREFIX = 'lr-';

Expand All @@ -16,6 +17,7 @@ export class Block extends BaseComponent {
/** @type {string | null} */
static StateConsumerScope = null;
static className = '';
requireCtxName = false;
allowCustomTemplate = true;

init$ = blockCtx();
Expand Down Expand Up @@ -137,7 +139,22 @@ export class Block extends BaseComponent {
this.constructor['template'] = null;
this.processInnerHtml = true;
}
super.connectedCallback();
if (this.requireCtxName) {
waitForAttribute({
element: this,
attribute: 'ctx-name',
onMutate: () => {
// async wait for ctx-name attribute to be set, needed for Angular because it sets attributes after mount
// TODO: should be moved to the symbiote core
super.connectedCallback();
},
onTimeout: () => {
console.error('Attribute `ctx-name` is required and it is not set.');
},
});
} else {
super.connectedCallback();
}
}

disconnectedCallback() {
Expand Down
1 change: 1 addition & 0 deletions abstract/SolutionBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ShadowWrapper } from '../blocks/ShadowWrapper/ShadowWrapper.js';
import { uploaderBlockCtx } from './CTX.js';

export class SolutionBlock extends ShadowWrapper {
requireCtxName = true;
init$ = uploaderBlockCtx(this);
_template = null;

Expand Down
2 changes: 2 additions & 0 deletions blocks/Config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { initialConfig } from './initialConfig.js';
import { sharedConfigKey } from '../../abstract/sharedConfigKey.js';
import { toKebabCase } from '../../utils/toKebabCase.js';
import { normalizeConfigValue } from './normalizeConfigValue.js';
import { waitForAttribute } from '../../utils/waitForAttribute.js';

Check warning on line 7 in blocks/Config/Config.js

View workflow job for this annotation

GitHub Actions / build

'waitForAttribute' is defined but never used

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import waitForAttribute.

const allConfigKeys = /** @type {(keyof import('../../types').ConfigType)[]} */ (Object.keys(initialConfig));

Expand Down Expand Up @@ -40,6 +41,7 @@ const attrStateMapping = /** @type {Record<keyof import('../../types').ConfigAtt

export class Config extends Block {
ctxOwner = true;
requireCtxName = true;

constructor() {
super();
Expand Down
1 change: 1 addition & 0 deletions blocks/DataOutput/DataOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { uploadFileGroup } from '@uploadcare/upload-client';

export class DataOutput extends UploaderBlock {
processInnerHtml = true;
requireCtxName = true;

init$ = {
...this.init$,
Expand Down
15 changes: 9 additions & 6 deletions blocks/ShadowWrapper/ShadowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { Block } from '../../abstract/Block.js';
import { waitForAttribute } from '../../utils/waitForAttribute.js';

const CSS_ATTRIBUTE = 'css-src';

Expand All @@ -22,17 +23,18 @@ export function shadowed(Base) {
return class extends Base {
renderShadow = true;
pauseRender = true;
requireCtxName = true;

shadowReadyCallback() {}

initCallback() {
super.initCallback();
this.setAttribute('hidden', '');

// async wait for attributes to be set, needed for Angular because it sets attributes after constructor
setTimeout(() => {
let href = this.getAttribute(CSS_ATTRIBUTE);
if (href) {
waitForAttribute({
element: this,
attribute: CSS_ATTRIBUTE,
onMutate: (href) => {
this.attachShadow({
mode: 'open',
});
Expand All @@ -53,11 +55,12 @@ export function shadowed(Base) {
};
// @ts-ignore TODO: fix this
this.shadowRoot.prepend(link);
} else {
},
onTimeout: () => {
console.error(
'Attribute `css-src` is required and it is not set. See migration guide: https://uploadcare.com/docs/file-uploader/migration-to-0.25.0/'
);
}
},
});
}
};
Expand Down
4 changes: 3 additions & 1 deletion blocks/UploadCtxProvider/UploadCtxProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UploaderBlock } from '../../abstract/UploaderBlock.js';

export class UploadCtxProvider extends UploaderBlock {}
export class UploadCtxProvider extends UploaderBlock {
requireCtxName = true;
}
4 changes: 2 additions & 2 deletions solutions/file-uploader/inline/demo.htm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2>Multiple sources</h2>
</style>

<!-- Uploader widget element: -->
<lr-file-uploader-inline css-src="./inline/index.css"> </lr-file-uploader-inline>
<lr-file-uploader-inline ctx-name="my-uploader" css-src="./inline/index.css"> </lr-file-uploader-inline>
</lr-live-html>

<h2>Single source</h2>
Expand Down Expand Up @@ -60,5 +60,5 @@ <h2>Single source</h2>
</style>

<!-- Uploader widget element: -->
<lr-file-uploader-inline current-activity css-src="./inline/index.css"> </lr-file-uploader-inline>
<lr-file-uploader-inline ctx-name="my-uploader" current-activity css-src="./inline/index.css"> </lr-file-uploader-inline>
</lr-live-html>
1 change: 1 addition & 0 deletions solutions/file-uploader/minimal/demo.htm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h2>Live example</h2>

<!-- Uploader widget element: -->
<lr-file-uploader-minimal
ctx-name="my-uploader"
css-src="./minimal/index.css"></lr-file-uploader-minimal>

<lr-data-output
Expand Down
4 changes: 2 additions & 2 deletions solutions/file-uploader/regular/demo.htm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h2>Button with modal window</h2>
</style>

<!-- Uploader widget element: -->
<lr-file-uploader-regular css-src="./regular/index.css">
<lr-file-uploader-regular ctx-name="my-uploader" css-src="./regular/index.css">
</lr-file-uploader-regular>
</lr-live-html>
<h2>Single source</h2>
Expand All @@ -37,6 +37,6 @@ <h2>Single source</h2>
</style>

<!-- Uploader widget element: -->
<lr-file-uploader-regular css-src="./regular/index.css">
<lr-file-uploader-regular ctx-name="my-uploader" css-src="./regular/index.css">
</lr-file-uploader-regular>
</lr-live-html>

0 comments on commit 7150f4c

Please sign in to comment.