Skip to content

Commit

Permalink
fix: use color attribute instead of fill
Browse files Browse the repository at this point in the history
  • Loading branch information
oktaysenkan committed Oct 7, 2024
1 parent cff5104 commit d27a5e5
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
11 changes: 9 additions & 2 deletions apps/react-native-app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React from "react";
import { StyleSheet, View } from "react-native";
import { Button, StyleSheet, View } from "react-native";
import { Monicon } from "@monicon/native";

export default function App() {
const [size, setSize] = React.useState(32);
return (
<View style={styles.container}>
<Monicon name="mdi:home" />
<Button title="Press me" onPress={(s) => setSize((s) => s + 2)} />
<Monicon name="mdi:home" color="red" size={size} />
<Monicon name="feather:activity" />
<Monicon name="logos:active-campaign" size={30} />
<Monicon name="logos:apache-superset-icon" />
<Monicon name="invalid:icon" />
<Monicon
size={24}
name="icon-park-outline:arrow-circle-right"
color="red"
/>
</View>
);
}
Expand Down
4 changes: 3 additions & 1 deletion apps/react-native-app/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [["@monicon/babel-plugin"]],
plugins: [
["@monicon/babel-plugin", { outputFileName: "react-native-app" }],
],
};
};
1 change: 1 addition & 0 deletions apps/react-native-app/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const configWithMonicon = withMonicon(config, {
"feather:alert-circle",
"logos:active-campaign",
"logos:apache-superset-icon",
"icon-park-outline:arrow-circle-right",
],
outputFileName: "react-native-app",
});
Expand Down
2 changes: 1 addition & 1 deletion apps/react-native-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"eject": "expo eject"
},
"dependencies": {
"@monicon/babel-plugin": "*",
"@monicon/metro": "*",
"@monicon/native": "*",
"@monicon/webpack": "*",
"@monicon/babel-plugin": "*",
"expo": "^49.0.21",
"expo-status-bar": "~1.7.0",
"react": "^18.2.0",
Expand Down
1 change: 1 addition & 0 deletions apps/react-native-app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = async function (env, argv) {
"feather:alert-circle",
"logos:active-campaign",
"logos:apache-superset-icon",
"icon-park-outline:arrow-circle-right",
],
outputFileName: "react-native-app",
})
Expand Down
14 changes: 4 additions & 10 deletions packages/icon-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export const getIconDetails = (

const parsed = parseSync(icon.svg);

let innerHtml = parsed.children.map((child) => stringify(child)).join("");

if (props.color) {
innerHtml = innerHtml.replace(/fill="([^"]+)"/, `fill="${props.color}"`);
parsed.attributes.color = props.color;
}

let innerHtml = parsed.children.map((child) => stringify(child)).join("");

const ratio = icon.width / icon.height;

const height = props.size ? props.size : icon.height;
Expand All @@ -58,13 +58,7 @@ export const getIconDetails = (

parsed.attributes = attributes;

icon.svg = icon.svg
.replace(/width="([^"]+)"/, `width="${width}"`)
.replace(/height="([^"]+)"/, `height="${height}"`);

if (props.color) {
icon.svg = icon.svg.replace(/fill="([^"]+)"/, `fill="${props.color}"`);
}
icon.svg = stringify(parsed);

return {
innerHtml,
Expand Down
4 changes: 2 additions & 2 deletions packages/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"typescript": "^5.3.3"
},
"dependencies": {
"html-react-parser": "^5.1.16",
"@monicon/core": "*",
"@monicon/icon-loader": "*"
"@monicon/icon-loader": "*",
"html-react-parser": "^5.1.16"
},
"peerDependencies": {
"react": ">=18.2.0",
Expand Down
12 changes: 6 additions & 6 deletions packages/native/src/Monicon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Icon } from "@monicon/core";
import type { XmlProps } from "react-native-svg";

export interface MoniconProps extends LoaderProps, XmlProps {
export interface MoniconProps extends LoaderProps, Omit<XmlProps, "xml"> {
name: LoaderProps["name"];
color?: LoaderProps["color"];
webProps?: React.SVGProps<SVGSVGElement>;
Expand Down Expand Up @@ -78,26 +78,26 @@ const getComponent = async (
return webIcon(details, props);
};

export const Monicon = ({ name, xml, color, size, ...props }: MoniconProps) => {
export const Monicon = React.memo((props: MoniconProps) => {
const [Component, setComponent] = React.useState<React.ReactNode | null>(
null
);

const renderIcon = async () => {
const renderIcon = React.useCallback(async () => {
const icons = await importIcons();

const details = getIconDetails({ name, color, size }, icons ?? {});
const details = getIconDetails(props, icons ?? {});

const component = await getComponent(details, props);

setComponent(component);
};
}, [props]);

React.useEffect(() => {
renderIcon();
}, [props]);

return Component;
};
});

export default Monicon;

0 comments on commit d27a5e5

Please sign in to comment.