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

FEAT: Icon parent wrapper element/component #720

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
29 changes: 28 additions & 1 deletion packages/react-icons/src/iconBase.tsx
Expand Up @@ -34,6 +34,12 @@ export interface IconBaseProps extends React.SVGAttributes<SVGElement> {
size?: string | number;
color?: string;
title?: string;
wrapper?:
| React.FunctionComponent<any>
| React.ComponentClass<any>
| string
| null;
wrapperClassName?: string;
}

export type IconType = (props: IconBaseProps) => JSX.Element;
Expand All @@ -48,7 +54,7 @@ export function IconBase(
if (props.className)
className = (className ? className + " " : "") + props.className;

return (
const icon = (
<svg
stroke="currentColor"
fill="currentColor"
Expand All @@ -70,6 +76,27 @@ export function IconBase(
{props.children}
</svg>
);

// if props.wrapper is null, explicitly skip Wrapper component.
// else, prioritize the wrapper component from props, then the context.
const wrapper =
props.wrapper === null ? undefined : props.wrapper || conf.wrapper;

// wrap component if wrapper is set.
if (wrapper) {
let wrapperClassName;
if (conf.wrapperClassName) wrapperClassName = conf.wrapperClassName;
if (props.wrapperClassName)
wrapperClassName =
(wrapperClassName ? wrapperClassName + " " : "") +
props.wrapperClassName;
return React.createElement(
wrapper,
{ className: wrapperClassName },
icon
);
}
return icon;
};

return IconContext !== undefined ? (
Expand Down
4 changes: 4 additions & 0 deletions packages/react-icons/src/iconContext.tsx
Expand Up @@ -6,6 +6,8 @@ export interface IconContext {
className?: string;
style?: React.CSSProperties;
attr?: React.SVGAttributes<SVGElement>;
wrapper?: React.FunctionComponent<any> | React.ComponentClass<any> | string;
wrapperClassName?: string;
}

export const DefaultContext: IconContext = {
Expand All @@ -14,6 +16,8 @@ export const DefaultContext: IconContext = {
className: undefined,
style: undefined,
attr: undefined,
wrapper: undefined,
wrapperClassName: undefined,
};

export const IconContext: React.Context<IconContext> =
Expand Down