Skip to content

Commit

Permalink
chore(customElementRegistry): add carbonElement decorator (carbon-des…
Browse files Browse the repository at this point in the history
…ign-system#10080)

### Related Ticket(s)

Closes carbon-design-system#9940 

### Description

This PR takes a new approach, and offers some advantages of carbon-design-system#9941.

While the other PR aims to separate class definitions from their registration in the browser, and should work well for teams or projects that wish to _extend_ C4IBM components, it does nothing to solve the problem for teams that are _double-consuming_ C4IBM components. For example, consider the widget team:

- They are building widgets using the react-wrapper version of the components
- When the react component imports the element, it brings along its `customElements.define()` method
- The widget is placed on a page that is already using the C4IBM CDN links, which _also_ bring along the `customElements.define()` method.
- Both the widget script and the CDN link are rolled up into separate scripts and whichever one runs second will encounter the unexpected error, not know how to handle that problem, and halt execution of the script, likely leaving large pieces of setup functionality unprocessed

This new approach offers a method for elements to _attempt_ to define themselves, but to gracefully handle the failure and to continue executing the remainder of the running script. The new decorator is a simple copy/paste from the LitElement `@customElement` decorator, with the `customElements.define` functions wrapped in a try/catch block.

As of right now, this only impacts one component and is simply a proof-of-concept.

### Changelog

**New**

- Adds a `@carbonElement()` decorator to the carbon-web-components package

**Changed**

- Edit the carbon-web-components package's accordion-item component to use @CarbonElement
  • Loading branch information
andy-blum authored Mar 27, 2023
1 parent 2632586 commit e34dcc6
Show file tree
Hide file tree
Showing 129 changed files with 929 additions and 268 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { classMap } from 'lit-html/directives/class-map';
import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import ChevronRight16 from '@carbon/icons/lib/chevron--right/16';
import FocusMixin from '../../globals/mixins/focus';
import Handle from '../../globals/internal/handle';
import { ACCORDION_ITEM_BREAKPOINT } from './defs';
import styles from './accordion.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export { ACCORDION_ITEM_BREAKPOINT };

Expand Down Expand Up @@ -189,7 +190,7 @@ class BXAccordionItem extends FocusMixin(LitElement) {
part="expando"
class="${prefix}--accordion__heading"
aria-controls="content"
aria-expanded="${String(Boolean(open))}"
aria-expanded="${open}"
@click="${handleClickExpando}"
@keydown="${handleKeydownExpando}">
${ChevronRight16({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import { forEach } from '../../globals/internal/collection-helpers';
import { ACCORDION_SIZE } from './defs';
import styles from './accordion.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export { ACCORDION_SIZE };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, customElement, LitElement } from 'lit-element';
import { html, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import styles from './breadcrumb.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { customElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import BXLink from '../link/link';
import styles from './breadcrumb.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { customElement, html } from 'lit-element';
import { html } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import OverflowMenuHorizontal16 from '@carbon/icons/lib/overflow-menu--horizontal/16';
import BXOverflowMenu from '../overflow-menu/overflow-menu';
import styles from './breadcrumb.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, customElement, LitElement } from 'lit-element';
import { html, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import styles from './breadcrumb.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { classMap } from 'lit-html/directives/class-map';
import { html, customElement } from 'lit-element';
import { html } from 'lit-element';
import ifNonNull from '../../globals/directives/if-non-null';
import BXButton from './button';
import styles from './button.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { classMap } from 'lit-html/directives/class-map';
import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import ifNonNull from '../../globals/directives/if-non-null';
import FocusMixin from '../../globals/mixins/focus';
import { BUTTON_KIND, BUTTON_SIZE, BUTTON_ICON_LAYOUT } from './defs';
import styles from './button.scss';
import HostListener from '../../globals/decorators/host-listener';
import HostListenerMixin from '../../globals/mixins/host-listener';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export { BUTTON_KIND, BUTTON_SIZE, BUTTON_ICON_LAYOUT };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { classMap } from 'lit-html/directives/class-map';
import { html, property, query, customElement, LitElement } from 'lit-element';
import { html, property, query, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import ifNonNull from '../../globals/directives/if-non-null';
import FocusMixin from '../../globals/mixins/focus';
import FormMixin from '../../globals/mixins/form';
import styles from './checkbox.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import { CODE_SNIPPET_TYPE } from './code-snippet';
import styles from './code-snippet.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { classMap } from 'lit-html/directives/class-map';
import { TemplateResult } from 'lit-html';
import { html, property, query, customElement, LitElement } from 'lit-element';
import { html, property, query, LitElement } from 'lit-element';
import ChevronDown16 from '@carbon/icons/lib/chevron--down/16';
import settings from 'carbon-components/es/globals/js/settings';
import FocusMixin from '../../globals/mixins/focus';
Expand All @@ -19,6 +19,7 @@ import {
} from '../copy-button/copy-button';
import { CODE_SNIPPET_COLOR_SCHEME, CODE_SNIPPET_TYPE } from './defs';
import styles from './code-snippet.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export { CODE_SNIPPET_COLOR_SCHEME, CODE_SNIPPET_TYPE };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { customElement } from 'lit-element';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';
import BXDropdownItem from '../dropdown/dropdown-item';
import styles from './combo-box.scss';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { TemplateResult } from 'lit-html';
import { html, property, query, customElement } from 'lit-element';
import { html, property, query } from 'lit-element';
import Close16 from '@carbon/icons/lib/close/16';
import { findIndex, forEach } from '../../globals/internal/collection-helpers';
import BXDropdown, { DROPDOWN_KEYBOARD_ACTION } from '../dropdown/dropdown';
import BXComboBoxItem from './combo-box-item';
import styles from './combo-box.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export {
DROPDOWN_COLOR_SCHEME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { classMap } from 'lit-html/directives/class-map';
import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import ifNonNull from '../../globals/directives/if-non-null';
import FocusMixin from '../../globals/mixins/focus';
import styles from './content-switcher.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import { forEach, indexOf } from '../../globals/internal/collection-helpers';
import { NAVIGATION_DIRECTION, CONTENT_SWITCHER_SIZE } from './defs';
import BXSwitch from './content-switcher-item';
import styles from './content-switcher.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

export { NAVIGATION_DIRECTION, CONTENT_SWITCHER_SIZE };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -10,11 +10,12 @@
import { classMap } from 'lit-html/directives/class-map';
import { TemplateResult } from 'lit-html';
import { ifDefined } from 'lit-html/directives/if-defined';
import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import Copy16 from '@carbon/icons/lib/copy/16';
import settings from 'carbon-components/es/globals/js/settings';
import FocusMixin from '../../globals/mixins/focus';
import styles from './copy-button.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { html, property, customElement, LitElement } from 'lit-element';
import { html, property, LitElement } from 'lit-element';
import settings from 'carbon-components/es/globals/js/settings';
import styles from './data-table.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* @license
*
* Copyright IBM Corp. 2019, 2022
* Copyright IBM Corp. 2019, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import settings from 'carbon-components/es/globals/js/settings';
import { html, property, query, customElement, LitElement } from 'lit-element';
import { html, property, query, LitElement } from 'lit-element';
import { TABLE_COLOR_SCHEME } from './defs';
import BXTableRow from './table-row';
import styles from './data-table.scss';
import { carbonElement as customElement } from '../../globals/decorators/carbon-element';

const { prefix } = settings;

Expand Down
Loading

0 comments on commit e34dcc6

Please sign in to comment.