Skip to content

Commit

Permalink
BUGFIX: Extract resource resolving magic out of ResourceIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Jul 8, 2024
1 parent 6bc407d commit 499181a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/neos-ui/src/Containers/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {fab} from '@fortawesome/free-brands-svg-icons';
import {far} from '@fortawesome/free-regular-svg-icons';
import {fas} from '@fortawesome/free-solid-svg-icons';
import '@fortawesome/fontawesome-svg-core/styles.css';
import {ResourceIconContext} from "@neos-project/react-ui-components/src/Icon/resourceIcon";

config.autoAddCss = false; // Dont insert the supporting CSS into the <head> of the HTML document
config.familyPrefix = 'neos-fa';
Expand All @@ -25,6 +26,7 @@ class Root extends PureComponent {

const containerRegistry = globalRegistry.get('containers');
const App = containerRegistry.get('App');
const createFromResourcePath = (path) => '/_Resources/Static/Packages/' + path.substr(11);

return (
<ErrorBoundary i18nRegistry={globalRegistry.get('i18n')}>
Expand All @@ -36,7 +38,9 @@ class Root extends PureComponent {
configuration={configuration}
routes={routes}
>
<App globalRegistry={globalRegistry} menu={menu}/>
<ResourceIconContext.Provider value={{createFromResourcePath}}>
<App globalRegistry={globalRegistry} menu={menu}/>
</ResourceIconContext.Provider>
</Neos>
</DndProvider>
</Provider>
Expand Down
15 changes: 13 additions & 2 deletions packages/react-ui-components/src/Icon/resourceIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ export interface ResourceIconProps extends Omit<IconProps, 'theme'> {
readonly theme?: ResourceIconTheme;
}

export const ResourceIconContext = React.createContext<{createFromResourcePath:(path: string) => string} | null>(null);

class ResourceIcon extends PureComponent<ResourceIconProps> {
public static readonly contextType = ResourceIconContext;

context!: React.ContextType<typeof ResourceIconContext>;

public static readonly defaultProps = defaultProps;

public render(): JSX.Element | null {
const {padded, theme, label, icon, className, color, size} = this.props;

if (!icon || icon.substr(0, 11) !== 'resource://') {
if (!icon?.startsWith('resource://')) {
return null;
}

if (!this.context) {
console.error('ResourceIconContext missing! Cannot resolve uri: ', icon);
return null;
}

const iconResourcePath = '/_Resources/Static/Packages/' + icon.substr(11);
const iconResourcePath = this.context.createFromResourcePath(icon);
const classNames = mergeClassNames(
theme!.icon,
className,
Expand Down

0 comments on commit 499181a

Please sign in to comment.