Improve compatibility for browsers that do not support ResizeObserver
or :where
selector
#5265
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
#5206 made Editable components use stylesheets for default styles instead of inline styles. However, it also inadvertently reduced compatibility with browsers that lack support for
ResizeObserver
and the:where
CSS selector (mainly Safari versions released before September 2020). The result was that browsers that did not supportResizeObserver
would throw aReferenceError
when rendering a placeholder element, and browsers that did not support:where
would not see default styles for Editable components.This PR adds a polyfill for
ResizeObserver
and a fallback for browsers that do not support:where
to fix both of those problems.Issue
Fixes: #5206 (comment)
Context
The latest major browser to add support for
ResizeObserver
is Safari in version 13.1, released March 2020. (https://caniuse.com/resizeobserver) On browsers that do not support it, aReferenceError
is thrown when a placeholder element is rendered. This problem is fixed by adding a polyfill forResizeObserver
: the npm package@juggle/resize-observer
. Note that the nativeResizeObserver
will be used if it exists.The latest major browser to add support for
:where
is Safari in version 14, released September 2020. (https://caniuse.com/mdn-css_selectors_where) Browsers that do not support:where
do not parse the default styles for Editable components and placeholder elements, and thus their default styles do not appear.Default stylesheets using
:where
were introduced in #5206 to give the default styles for Editable components and placeholder elements a specificity of 0, which allows for user-defined stylesheets to override them by simply adding their own class to the Editable component. I do not know of a way to implement this behavior without support for:where
or CSS layers, so I instead opted to just fix the default styles not being applied.The fallback for the
:where
selector is implemented by checking support for it using a@supports
rule. If there is no support, the default styles for Editable components and placeholder elements is set without:where
. This means that after this change, browsers that do not support:where
will see default styles, but the specificity of the default styles will be high enough that a user-defined stylesheet with a class for an Editable component will not override the default style. However, inline styles on the Editable component should still override the default styles.I tested these changes on Firefox 68.9.0 ESR, which supports neither
ResizeObserver
nor:where
, and both problems appear to be fixed.Note: it is unclear to me which browser versions Slate intends to support, but the FAQ only rules out support for IE11, and in the issue linked above at least one person has expressed a desire for Slate to support pre-13.1 Safari.
Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)