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

:dir() support #4880

Merged
merged 8 commits into from
Oct 16, 2017
Merged

:dir() support #4880

merged 8 commits into from
Oct 16, 2017

Conversation

dfreedm
Copy link
Member

@dfreedm dfreedm commented Oct 11, 2017

Reference Issue

Polymer 2.x fix for #4860

Goal

Allows for native and polyfilled ShadowDOM elements to use :dir CSS selector to provide text directionality based layouts.

Details

  • Modifies Polymer.ElementMixin to provide override points for stylesheet modification.
  • Elements can opt out of the global page text direction by setting the dir attribute directly in ready().
  • Automatic left-to-right or right-to-left styling is sync'd with the <html> element
  • Applications must set <html dir="ltr"> or <html dir="rtl"> to sync direction
  • Opting out of the global direction styling is permanent

ShadyCSS version of the :dir transform is not good enough :(
Adapt scoped keyframes test for Safari 11 still failing on https://bugs.webkit.org/show_bug.cgi?id=166748
@dfreedm dfreedm requested a review from sorvell October 11, 2017 17:57
function setRTL(instance) {
if (!instance.__origRTLStatus) {
const el = /** @type {!HTMLElement} */(instance);
if (document.documentElement.getAttribute('dir') === 'rtl') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's capture this state so we can make this application function faster.

* Element class mixin that allows elements to use the `:dir` CSS Selector to have
* text direction specific styling.
*
* With this mixin, any stylesheet provided in the template will be transform `:dir` into
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will transform

* directly in `ready()`.
*
* Caveats:
* - Automatic left-to-right or right-to-left styling is sync'd with the `<html>` element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<html> element only, setting the dir attribute on an element further up the DOM tree will have no effect.

*
* Caveats:
* - Automatic left-to-right or right-to-left styling is sync'd with the `<html>` element
* - Applications must set `<html dir="ltr">` or `<html dir="rtl">` to sync direction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this as the first caveat and then say that changing dir on html is supported.

* `:host([dir])` and sync direction with the page via the element's `dir` attribute.
*
* Elements can opt out of the global page text direction by setting the `dir` attribute
* directly in `ready()`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or in HTML?

* @extends {base}
* @implements {Polymer_ElementMixin}
*/
const elementBase = Polymer.ElementMixin(base);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about removing this dependency and requiring users to at least also use PropertyAccessors?

finalizeTemplate(/** @type {!PolymerElement} */(this.__proto__), this._template, baseURI,
/**@type {!HTMLElement}*/(this).localName);
}
this.constructor._finalizeTemplate(/** @type {!PolymerElement} */(this.__proto__), this._template,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a static now, let's not pass in the prototype.

* @override
* @suppress {missingProperties} Interfaces in closure do not inherit statics, but classes do
*/
static _processStyleText(is, template, baseURI) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only enable the runtime element support if we find dir in the css.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/**
* @suppress {invalidCasts} Closure doesn't understand that `this` is an HTMLElement
*/
ready() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make these runtime features opt-in via a flag.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is still needed if we use the presence of :dir to activate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok. They are different and an element could have either or both.

* @appliesMixin Polymer.ElementMixin
* @memberof Polymer
*/
Polymer.DirMixin = Polymer.dedupingMixin((base) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mix this into LegacyElement so we have hybrid support since 1.x has dir support now.

- Extend PropertyAccessors for greater flexibility
- Detect if `:dir` is used in order to activate runtime features
- Add to LegacyElement for 1.x compatibility
- clean up `ElementMixin._finalizeTemplate`
const DIR_INSTANCES = [];

/** @type {MutationObserver} */
let OBSERVER = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not all caps for variables?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

/**
* @suppress {invalidCasts} Closure doesn't understand that `this` is an HTMLElement
*/
ready() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok. They are different and an element could have either or both.

/** @suppress {missingProperties} Closure's understanding of prototypes is bad */
connectedCallback() {
if (elementBase.prototype.connectedCallback) {
/** @type {!Polymer_ElementMixin} */(elementBase).prototype.connectedCallback.call(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not super.connectedCallback() (and same for disconnected...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropertyAccessors doesn't have connectedCallback or disconnectedCallback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically if you find it, call super. rather than .call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure

/** @type {!Polymer_ElementMixin} */(elementBase).prototype.connectedCallback.call(this);
}
if (this.constructor.__activateDir) {
takeRecords();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this will fly (same in disconnected). We cannot update all elements as takeRecords does when one is connected

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just means I'll have to pull document direction on connected.

subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../utils/style-gather.html">
<link rel="import" href="../utils/resolve-url.html">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like the imports above are used, remove?

@dfreedm dfreedm merged commit e9e2cda into master Oct 16, 2017
@dfreedm dfreedm deleted the native-dir branch October 16, 2017 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants