-
Notifications
You must be signed in to change notification settings - Fork 377
feat(icons): investigating pf-rh icon hotswapping #12210
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
kmcfaul
wants to merge
2
commits into
patternfly:main
Choose a base branch
from
kmcfaul:dual-icons
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.
+496
−59
Open
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,45 +4,96 @@ export interface SVGPathObject { | |
| path: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| export interface IconDefinition { | ||
| name?: string; | ||
| width: number; | ||
| height: number; | ||
| svgPath: string | SVGPathObject[]; | ||
| svgPathData: string | SVGPathObject[]; | ||
| xOffset?: number; | ||
| yOffset?: number; | ||
| svgClassName?: string; | ||
| } | ||
|
|
||
| export interface CreateIconProps { | ||
| name?: string; | ||
| icon?: IconDefinition; | ||
| rhUiIcon?: IconDefinition | null; | ||
| } | ||
|
|
||
| export interface SVGIconProps extends Omit<React.HTMLProps<SVGElement>, 'ref'> { | ||
| title?: string; | ||
| className?: string; | ||
| /* Indicates the icon should render using alternate svg data for unified theme */ | ||
| set?: 'default' | 'unified'; | ||
| } | ||
|
|
||
| let currentId = 0; | ||
| const canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); | ||
|
|
||
| /** | ||
| * Factory to create Icon class components for consumers | ||
| */ | ||
| export function createIcon({ | ||
| name, | ||
| xOffset = 0, | ||
| yOffset = 0, | ||
| width, | ||
| height, | ||
| svgPath, | ||
| svgClassName | ||
| }: IconDefinition): React.ComponentClass<SVGIconProps> { | ||
| return class SVGIcon extends Component<SVGIconProps> { | ||
| export function createIcon({ name, icon, rhUiIcon = null }: CreateIconProps): React.ComponentClass<SVGIconProps> { | ||
| return class SVGIcon extends Component<SVGIconProps, { themeClassVersion: number }> { | ||
| static displayName = name; | ||
|
|
||
| id = `icon-title-${currentId++}`; | ||
| private observer: MutationObserver | null = null; | ||
|
|
||
| constructor(props: SVGIconProps) { | ||
| super(props); | ||
| this.state = { themeClassVersion: 0 }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| if (rhUiIcon !== null && canUseDOM) { | ||
| this.observer = new MutationObserver((mutations) => { | ||
| for (const mutation of mutations) { | ||
| if (mutation.type === 'attributes' && mutation.attributeName === 'class') { | ||
| const target = mutation.target as HTMLElement; | ||
| const hadClass = (mutation.oldValue || '').includes('pf-v6-theme-unified'); | ||
| const hasClass = target.classList.contains('pf-v6-theme-unified'); | ||
|
|
||
| if (hadClass !== hasClass && this.props.set === undefined) { | ||
| this.setState((prevState) => ({ | ||
| themeClassVersion: prevState.themeClassVersion + 1 | ||
| })); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| this.observer.observe(document.documentElement, { | ||
| attributes: true, | ||
| attributeFilter: ['class'], | ||
| attributeOldValue: true | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| if (this.observer) { | ||
| this.observer.disconnect(); | ||
| this.observer = null; | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const { title, className: propsClassName, ...props } = this.props; | ||
| const { title, className: propsClassName, set, ...props } = this.props; | ||
|
|
||
| const shouldUseAltData = | ||
| rhUiIcon !== null && | ||
| (set === 'unified' || | ||
| (set === undefined && canUseDOM && document.documentElement.classList.contains('pf-v6-theme-unified'))); | ||
|
|
||
| const iconData = shouldUseAltData ? rhUiIcon : icon; | ||
| const { xOffset, yOffset, width, height, svgClassName, svgPathData } = iconData ?? {}; | ||
| const _xOffset = xOffset ?? 0; | ||
| const _yOffset = yOffset ?? 0; | ||
|
|
||
| const hasTitle = Boolean(title); | ||
| const viewBox = [xOffset, yOffset, width, height].join(' '); | ||
| const viewBox = [_xOffset, _yOffset, width, height].join(' '); | ||
|
|
||
| const classNames = ['pf-v6-svg']; | ||
| if (svgClassName) { | ||
|
|
@@ -52,6 +103,14 @@ export function createIcon({ | |
| classNames.push(propsClassName); | ||
| } | ||
|
|
||
| const svgPaths = Array.isArray(svgPathData) ? ( | ||
| svgPathData.map((pathObject, index) => ( | ||
| <path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} /> | ||
| )) | ||
| ) : ( | ||
| <path d={svgPathData as string} /> | ||
| ); | ||
|
Comment on lines
+106
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle undefined If Add fallback- const svgPaths = Array.isArray(svgPathData) ? (
+ const svgPaths = !svgPathData ? null : Array.isArray(svgPathData) ? (
svgPathData.map((pathObject, index) => (
<path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} />
))
) : (
<path d={svgPathData as string} />
);🤖 Prompt for AI Agents |
||
|
|
||
| return ( | ||
| <svg | ||
| className={classNames.join(' ')} | ||
|
|
@@ -65,15 +124,76 @@ export function createIcon({ | |
| {...(props as Omit<React.SVGProps<SVGElement>, 'ref'>)} // Lie. | ||
| > | ||
| {hasTitle && <title id={this.id}>{title}</title>} | ||
| {Array.isArray(svgPath) ? ( | ||
| svgPath.map((pathObject, index) => ( | ||
| <path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} /> | ||
| )) | ||
| ) : ( | ||
| <path d={svgPath} /> | ||
| )} | ||
| {svgPaths} | ||
| </svg> | ||
| ); | ||
|
|
||
| // Alternate CSS method tinkering | ||
| // TODO: remove or refactor to use this method | ||
| // Below works for paths, but not viewbox without needing the MutationObserver which would be nice to not have when using CSS method | ||
| // May be able to use two separate svgs instead of paths instead if going this route | ||
|
|
||
| // const defaultSvgPathData = Array.isArray(icon?.svgPathData) ? ( | ||
| // icon?.svgPathData.map((pathObject, index) => ( | ||
| // <path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} /> | ||
| // )) | ||
| // ) : ( | ||
| // <path d={icon?.svgPathData as string} /> | ||
| // ); | ||
|
|
||
| // const rhUiSvgPathData = | ||
| // rhUiIcon?.svgPathData && Array.isArray(rhUiIcon?.svgPathData) ? ( | ||
| // rhUiIcon?.svgPathData.map((pathObject, index) => ( | ||
| // <path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} /> | ||
| // )) | ||
| // ) : ( | ||
| // <path d={rhUiIcon?.svgPathData as string} /> | ||
| // ); | ||
|
|
||
| // const finalSvgPath = | ||
| // rhUiIcon !== null ? ( | ||
| // <> | ||
| // <g className="pf-icon">{defaultSvgPathData}</g> | ||
| // <g className="rh-icon">{rhUiSvgPathData}</g> | ||
| // </> | ||
| // ) : ( | ||
| // pfSvgPathData | ||
| // ); | ||
|
|
||
| // return ( | ||
| // <svg | ||
| // className={classNames.join(' ')} | ||
| // viewBox={viewBox} | ||
| // fill="currentColor" | ||
| // aria-labelledby={hasTitle ? this.id : null} | ||
| // aria-hidden={hasTitle ? null : true} | ||
| // role="img" | ||
| // width="1em" | ||
| // height="1em" | ||
| // {...(props as Omit<React.SVGProps<SVGElement>, 'ref'>)} // Lie. | ||
| // > | ||
| // {hasTitle && <title id={this.id}>{title}</title>} | ||
| // <style type="text/css"> | ||
| // {/* Testing CSS visibility switching */} | ||
| // {/* TODO: move to css file, add to global styles */} | ||
| // {` | ||
| // .pf-v6-theme-unified { | ||
| // .pf-icon { | ||
| // display: none; | ||
| // } | ||
| // .rh-icon { | ||
| // display: block; | ||
| // } | ||
| // } | ||
| // .rh-icon { | ||
| // display: none; | ||
| // } | ||
|
|
||
| // `} | ||
| // </style> | ||
| // {finalSvgPath} | ||
| // </svg> | ||
| // ); | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
MutationObserver per icon instance may degrade performance at scale.
Each icon instance creates its own
MutationObserverwatchingdocument.documentElement. With hundreds of icons on a page, this creates hundreds of observers that all fire on every class change, each triggering asetStateand potential re-render.Consider a shared singleton observer pattern:
Singleton observer approach
Then in the component, subscribe and unsubscribe via
componentDidMount/componentWillUnmount.🤖 Prompt for AI Agents