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

BUGFIX: Render table dropdowns with correct icons #3898

Open
wants to merge 3 commits into
base: 8.3
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUGFIX: Render table dropdowns with correct icons
The icons were broken and the fallback title was not aligned pretty well. So the SVG markup string will be transformed now to an data-uri and this can be used as image sorce.
markusguenther committed Dec 10, 2024
commit 46e12b81911051e91709b72af090142591b1e372
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import {$get} from 'plow-js';

import {neos} from '@neos-project/neos-ui-decorators';
import {svgToDataUri} from '@neos-project/utils-helpers';
import ckeIcons from './icons';

import style from './TableDropDown.module.css';
@@ -40,12 +41,14 @@ export default class TableDropDownButton extends PureComponent {
}

render() {
const iconDataUri = svgToDataUri(ckeIcons[this.props.icon]);
console.log({iconDataUri});
return (
<DropDown
padded={false}
>
<DropDown.Header title={this.props.i18nRegistry.translate(this.props.tooltip)}>
<img style={{verticalAlign: 'text-top'}} src={ckeIcons[this.props.icon]} alt={this.props.i18nRegistry.translate(this.props.tooltip)} />
<img style={{verticalAlign: 'text-top'}} src={iconDataUri} alt={this.props.i18nRegistry.translate(this.props.tooltip)} />
Copy link
Member

Choose a reason for hiding this comment

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

öhm do i get that right that we want to have the svgs as data-url and not as raw content string?
That can be done via the build setup. For the neos ui i introduced the convention that files ending with .dataurl.svg will get that treatment via the build setup already introduced via:

#3836

Copy link
Member Author

Choose a reason for hiding this comment

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

The issue here was that the content of this.props.icon is just the SVG markup.
And that in a src tag is not working, and I bet that the build pipeline also doesn't handle it.

</DropDown.Header>
<DropDown.Contents className={style.contents} scrollable={false}>
{this.props.options.map(item => item.type === 'checkBox' ? (
2 changes: 2 additions & 0 deletions packages/utils-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import isEmail from './isEmail';
import {isUri} from './isUri';
import isEqualSet from './isEqualSet';
import isNil from './isNil';
import svgToDataUri from './svgToDataUri';

export {
delay,
@@ -23,6 +24,7 @@ export {
isEqualSet,
stripTags,
stripTagsEncoded,
svgToDataUri,
cancelIdleCallback,
requestIdleCallback
};
47 changes: 47 additions & 0 deletions packages/utils-helpers/src/svgToDataUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const REGEX = {
Copy link
Member

Choose a reason for hiding this comment

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

lol 😅 i hope you dindt had to write this yourself? :D I think more simple it would be to just use base64 decode and done :D

Edit: see comment above - i believe we dont need this at all hopefully

Copy link
Member Author

Choose a reason for hiding this comment

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

Can discuss other solutions of course :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I simplified that and now use base64 for the inline dataUri SVG stuff.

whitespace: /\s+/g,
urlHexPairs: /%[\dA-F]{2}/g,
quotes: /"/g
};

// Function to collapse whitespace in a string
const collapseWhitespace = (str: string): string =>
str.trim().replace(REGEX.whitespace, ' ');

// Function to encode data for a URI payload
const dataURIPayload = (string: string): string =>
encodeURIComponent(string).replace(REGEX.urlHexPairs, specialHexEncode);

// Function to handle special hex encoding
const specialHexEncode = (match: string): string => {
switch (match) {
case '%20':
return ' ';
case '%3D':
return '=';
case '%3A':
return ':';
case '%2F':
return '/';
default:
return match.toLowerCase(); // Compresses better
}
};

// Function to convert an SVG string to a tiny data URI
const svgToDataUri = (svgString: string): string => {
// Strip the Byte-Order Mark if the SVG has one
if (svgString.charCodeAt(0) === 0xfeff) {
svgString = svgString.slice(1);
}

const body = collapseWhitespace(svgString);
return `data:image/svg+xml,${dataURIPayload(body)}`;
};

// Add a static method to handle srcset conversions
svgToDataUri.toSrcset = (svgString: string): string =>
svgToDataUri(svgString).replace(/ /g, '%20');

// Export the function as the default export
export default svgToDataUri;