-
Notifications
You must be signed in to change notification settings - Fork 86
fix: restore icon size workaround #10296
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
Open
DiegoCardoso
wants to merge
3
commits into
main
Choose a base branch
from
fix/icon/restore-safari-workaround
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2021 - 2025 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import type { Constructor } from '@open-wc/dedupe-mixin'; | ||
|
||
/** | ||
* Mixin which enables the font icon sizing fallback for browsers that do not support CSS Container Queries. | ||
* The mixin does nothing if the browser supports CSS Container Query units for pseudo elements. | ||
*/ | ||
export declare function IconFontSizeMixin<T extends Constructor<HTMLElement>>( | ||
base: T, | ||
): Constructor<IconFontSizeMixinClass> & T; | ||
|
||
export declare class IconFontSizeMixinClass {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2021 - 2025 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js'; | ||
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js'; | ||
import { needsFontIconSizingFallback } from './vaadin-icon-helpers.js'; | ||
|
||
const usesFontIconSizingFallback = needsFontIconSizingFallback(); | ||
|
||
if (usesFontIconSizingFallback) { | ||
registerStyles( | ||
'vaadin-icon', | ||
css` | ||
:host::after, | ||
:host::before { | ||
font-size: var(--_vaadin-font-icon-size); | ||
} | ||
`, | ||
'vaadin-icon-font-size-mixin-styles', | ||
); | ||
} | ||
|
||
/** | ||
* Mixin which enables the font icon sizing fallback for browsers that do not support CSS Container Queries. | ||
* The mixin does nothing if the browser supports CSS Container Query units for pseudo elements. | ||
* | ||
* @polymerMixin | ||
*/ | ||
export const IconFontSizeMixin = (superclass) => | ||
!usesFontIconSizingFallback | ||
? superclass | ||
: class extends ResizeMixin(superclass) { | ||
static get observers() { | ||
return ['__iconFontSizeMixinfontChanged(iconClass, char, ligature)']; | ||
} | ||
|
||
/** @protected */ | ||
ready() { | ||
super.ready(); | ||
|
||
// Update once initially to avoid a fouc | ||
this.__updateFontIconSize(); | ||
} | ||
|
||
/** @private */ | ||
__iconFontSizeMixinfontChanged(_iconClass, _char, _ligature) { | ||
// Update when iconClass, char or ligature changes | ||
this.__updateFontIconSize(); | ||
} | ||
|
||
/** | ||
* @protected | ||
* @override | ||
*/ | ||
_onResize() { | ||
// Update when the element is resized | ||
this.__updateFontIconSize(); | ||
} | ||
|
||
/** | ||
* Updates the --_vaadin-font-icon-size CSS variable value if font icons are used. | ||
* | ||
* @private | ||
*/ | ||
__updateFontIconSize() { | ||
if (this.char || this.iconClass || this.ligature) { | ||
const { paddingTop, paddingBottom, height } = getComputedStyle(this); | ||
const fontIconSize = parseFloat(height) - parseFloat(paddingTop) - parseFloat(paddingBottom); | ||
this.style.setProperty('--_vaadin-font-icon-size', `${fontIconSize}px`); | ||
} | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2016 - 2025 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
|
||
import { isSafari } from '@vaadin/component-base/src/browser-utils.js'; | ||
|
||
/** | ||
* Checks if the current browser supports CSS Container Query units for pseudo elements. | ||
* i.e. if the fix for https://bugs.webkit.org/show_bug.cgi?id=253939 is available. | ||
*/ | ||
export function supportsCQUnitsForPseudoElements() { | ||
const testStyle = document.createElement('style'); | ||
testStyle.textContent = ` | ||
.vaadin-icon-test-element { | ||
container-type: size; | ||
height: 2px; | ||
visibility: hidden; | ||
position: fixed; | ||
} | ||
|
||
.vaadin-icon-test-element::before { | ||
content: ''; | ||
display: block; | ||
height: 100cqh; | ||
`; | ||
const testElement = document.createElement('div'); | ||
testElement.classList.add('vaadin-icon-test-element'); | ||
|
||
const shadowParent = document.createElement('div'); | ||
shadowParent.attachShadow({ mode: 'open' }); | ||
shadowParent.shadowRoot.innerHTML = '<slot></slot>'; | ||
shadowParent.append(testElement.cloneNode()); | ||
|
||
document.body.append(testStyle, testElement, shadowParent); | ||
|
||
const needsFallback = [...document.querySelectorAll('.vaadin-icon-test-element')].find( | ||
(el) => getComputedStyle(el, '::before').height !== '2px', | ||
); | ||
|
||
testStyle.remove(); | ||
testElement.remove(); | ||
shadowParent.remove(); | ||
return !needsFallback; | ||
} | ||
|
||
/** | ||
* Checks if the current browser needs a fallback for sizing font icons instead of relying on CSS Container Queries. | ||
*/ | ||
export function needsFontIconSizingFallback() { | ||
if (!CSS.supports('container-type: inline-size')) { | ||
// The browser does not support CSS Container Queries at all. | ||
return true; | ||
} | ||
if (!isSafari) { | ||
// Browsers other than Safari support CSS Container Queries as expected. | ||
return false; | ||
} | ||
// Check if the browser does not support CSS Container Query units for pseudo elements. | ||
return !supportsCQUnitsForPseudoElements(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use Lit lifecycle callback instead:
This should make it unnecessary to call method in
ready()
, asupdated()
runs synchronouslyready()
, unlike complex observers that run before, and therefore single invocation for method should be enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Done.