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

Proposal/RFC scoped styles with custom elements #1764

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 26 additions & 20 deletions js/src/components/connected-icon-label/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { Flex, FlexItem } from '@wordpress/components';
import GridiconCheckmarkCircle from 'gridicons/dist/checkmark-circle';

/**
* Internal dependencies
*/
import './index.scss';
import { css, defineShadowStylesHost } from '.~/utils/defineShadowStylesHost';

const ConnectedIconLabel = ( props ) => {
const { className } = props;
export const ConnectedIconLabelStyle = css`
:host {
display: inline-flex;
fill: currentcolor;
color: #23a713;
gap: var( --wp-grid-gap, 4px );
align-items: center;

return (
<Flex
className={ classnames( 'gla-connected-icon-label', className ) }
align="center"
gap={ 1 }
>
<FlexItem>
<GridiconCheckmarkCircle />
</FlexItem>
<FlexItem>
{ __( 'Connected', 'google-listings-and-ads' ) }
</FlexItem>
</Flex>
);
};
direction: row;
justify-content: space-between;
}
::slotted( svg ) {
display: block;
}
`;

defineShadowStylesHost( 'gla-connected-icon-label', [
ConnectedIconLabelStyle,
] );

const ConnectedIconLabel = ( { className, ...props } ) => (
<gla-connected-icon-label class={ className } { ...props }>
<GridiconCheckmarkCircle />
{ __( 'Connected', 'google-listings-and-ads' ) }
</gla-connected-icon-label>
);

export default ConnectedIconLabel;
8 changes: 0 additions & 8 deletions js/src/components/connected-icon-label/index.scss

This file was deleted.

50 changes: 50 additions & 0 deletions js/src/utils/defineShadowStylesHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Utility to define scoped styles using a custom element with a shadow root.
*/
/* global CSSStyleSheet, HTMLElement */
/**
* A template literal tag to create stylesheet objects.
*
* It's overly simplify, in future we may introduce more features, like:
* - Safari support
* - lazy constructing - not to create object untill the first instance is used
* - safe rules nesting
* - etc.
*
* @param {TemplateStringsArray} strings
* @param {Array} values
* @return {CSSStyleSheet} Constructed stylesheet.
*/
export const css = ( strings, ...values ) => {
const cssText =
strings.length === 1
? strings[ 0 ]
: values.reduce(
( accumulator, value, index ) =>
accumulator + value + strings[ index + 1 ],
strings[ 0 ]
);
const sheet = new CSSStyleSheet();
sheet.replaceSync( cssText );
return sheet;
};

/**
* Creates a custom element that will act as the host for given styles.
*
* @param {string} name Valid custom element name.
* @param {Array<CSSStyleSheet>} styles Style sheets to be adoptes by the host.
*/
export const defineShadowStylesHost = ( name, styles ) => {
window.customElements.define(
name,
class extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow( { mode: 'open' } );
shadowRoot.innerHTML = `<slot></slot>`;
shadowRoot.adoptedStyleSheets = styles;
}
}
);
};