Skip to content

Commit

Permalink
fix: provide symbol attributes to svg root container
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Pronin committed Jul 31, 2024
1 parent ae19310 commit e425b52
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/icons-scripts/scripts/icons-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { dashToCamel, sortArrayAlphabetically, longestCommonPrefix } = require('.
* @property {string} componentName
* @property {string} content
* @property {Icon[]} subcomponents
* @property {Record<string, string>} [attrs]
* @property {boolean} [isSubcomponent]
*/

Expand Down Expand Up @@ -211,6 +212,12 @@ async function prepareIconMapEntity(compiler, icon, optimizeFn) {
const symbol = await compiler.addSymbol({ content, id: icon.filename, path: '' });

const viewBox = symbol.viewBox;
// Список поддерживаемых аттрибутов, которые дублируются с symbol-элемента на svg-элемент, который ссылается на symbol
const attrs = Object.fromEntries(
Object.entries({
preserveAspectRatio: symbol.tree[0]?.attrs.preserveAspectRatio,
}).filter(([, value]) => value !== undefined),
);
const width = viewBox.split(' ')[2];
const height = viewBox.split(' ')[3];

Expand All @@ -220,6 +227,7 @@ async function prepareIconMapEntity(compiler, icon, optimizeFn) {
height,
content,
viewBox,
attrs: Object.keys(attrs).length ? attrs : undefined,
subcomponents,
symbolId: symbol.id,
symbol: symbol.render(),
Expand Down
2 changes: 2 additions & 0 deletions packages/icons-scripts/scripts/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function generateIcons(options) {
height,
dirname,
size,
attrs,
subcomponents,
isSubcomponent,
} = icon;
Expand All @@ -127,6 +128,7 @@ function generateIcons(options) {
// Превращаем svg-файл в ts-файл в виде строки
const reactSource = createReactIcon({
id: symbolId,
attrs,
width,
height,
viewBox,
Expand Down
2 changes: 2 additions & 0 deletions packages/icons-scripts/scripts/output/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { dashToCamel } = require('../utils');

function createReactIcon({
id,
attrs,
width,
height,
viewBox,
Expand Down Expand Up @@ -52,6 +53,7 @@ export const ${componentName} = makeIcon<${componentName}Props, ${typeAssigns}>(
${height},
${!!deprecated},
${replacement ? `'${replacement}'` : undefined}
${attrs ? `, ${JSON.stringify(attrs)}` : ''}
);
${assigns}
`;
Expand Down
2 changes: 2 additions & 0 deletions packages/icons-sprite/src/SvgIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function makeIcon<Props extends SvgIconProps = SvgIconProps, Subcomponent
height: number,
deprecated?: boolean,
replacement?: string,
attrs?: Record<string, string>,
): React.FC<Props> & Subcomponents {
let isMounted = false;
function mountIcon() {
Expand All @@ -95,6 +96,7 @@ export function makeIcon<Props extends SvgIconProps = SvgIconProps, Subcomponent

return (
<SvgIcon
{...attrs}
{...props}
viewBox={viewBox}
id={id}
Expand Down

0 comments on commit e425b52

Please sign in to comment.