$ cnpm install @design-systems/utils
Helper utilities for developing react components.
npm i @design-systems/utils
# with yarn
yarn add @design-systems/utils
import Utils from '@design-systems/utils';
arrayify (function)Normalize a value to an array.
Parameters:
T | T[]) - The value to potentially convert to an arrayarrayify('a'); // = ['a']
arrayify(['a']); // = ['a']
fromEntries (function)Object.fromEntries ponyfill while we wait for better babel support.
Parameters:
[string, any][]) - Array of object entries.fromEntries([['key', 'value']]);
Omit (type)Omit keys from a type.
Omit<{foo: string, bar: string}, 'foo'>
// = { bar: string }
omit (function)Omit keys from an object.
Parameters:
Props) - The object to omit props fromProp[]) - A list of keys to omitHere is a simple example
const result = omit({ foo: 'a', bar: 'b' }, 'foo');
// result = { bar: 'b' }
Here is a another example
const result = omit({ baz: 'a', bar: 'b' }, 'baz');
// result = { bar: 'b' }
getProp (function)Attempt to retrieve a prop from a react node.
Parameters:
ReactNode) - The react node to get props fromstring) - The name of the prop to getgetProp(child, 'id');
DisplayNamed (interface)Members:
string) - The name various dev tools should use to display the componentdisplayName (function)Set a displayName on a component. This name is used in various dev tools.
Parameters:
T) - The component to set the display name onstring) - The display name for the componentdisplayName(Component, 'MyCoolComponent');
isReactInstanceOf (function)Determine whether a HTML element is an instance of a React component.
Parameters:
Renderable<Props>) - The component to check forany)returns: boolean
isReactInstanceOf(child, MyComponent);
WINDOW_RESIZE_DELAY (variable)debounce (function)Only call a function once every 'wait" seconds.
Parameters:
Base) - the callback to debouncenumber) - how long to wait until to run the callbackboolean) - run the callback immediatelyreturns: Base
const onClick = debounce(() => console.log('I was clicked'), 1000);
logger (variable)Logger A logging utility which maps to console in development but is a no-op in production.
Require (type)Mark some keys of an interface as required
Properties:
type Example = { foo?: string; bar?: string };
type WithFooRequired = Require<Example, 'foo'>;
Element (type)Get all of the props for an HTML element. Used to easily type the rest props of a component.
Properties:
interface CardProps extends Element<'div'> {
isRound?: boolean;
}
const Card: React.FC<CardProps> = ({ isRound, children, ...html }) => (
<div {...html} style={{ borderRadius: isRound ? '4px' : 0 }}>
{children}
</div>
);
const Card: React.FC<Element<'div'>> = ({ children, ...html }) => (
<div {...html}>{children}</div>
);
Never (type)Create an interface that has all the properties of the input interface set to 'never'.
Properties:
type A = { a: string };
type B = { b: string };
type C = A & Never<B>;
const test: C = {
a: 'foo',
b: 'bar' // <- This line will create an error
};
OneOf (type)Create an interface that only accepts one of the two provided interface
type A = { a: string };
type B = { b: string };
type C = OneOf<A, B>;
const test: C = {
a: 'foo',
b: 'bar' // <- This line will create an error
};
OneOf3 (type)Create an interface that only accepts one of the three provided interface
type A = { a: string };
type B = { b: string };
type C = { c: string };
type D = OneOf<A, B, C>;
const test: D = {
a: 'foo',
c: 'bar' // <- This line will create an error
};
createInstanceIfDefined (function)Create an instance of the component only if the element is defined.
Parameters:
ReactNode) - The node to check if it's definedComponentType<{}>) - The component to wrap the node inconst child = 'Foo';
createInstanceIfDefined(child, Wrapper);
// <Wrapper>'Foo'</Wrapper>
const other = null;
createInstanceIfDefined(other, Wrapper);
// undefined
SLOT_KEY (variable)getSlotToken (function)Gets the token to represent the slot on an element
Parameters:
any) - The React component or element you want to get the slot token fromreturns: any
isSlotOf (function)Check to see if a child component is an instance of the given slot
Parameters:
any) - The React child component instance to testsymbol | ComponentClass<any, any> | FunctionComponent<any>) - The React Component or Slot ID (Symbol) to test againstreturns: boolean
forwardWithSlots (function)Forward a ref and make the returned component slottable.
Parameters:
ForwardRefRenderFunction<RefType, PropType>) - Same props you give to React.forwardRefexport const SlottedComponentWithRef = forwardWithSlots<
HTMLDivElement,
ContentCardProps,
SubComponents
>((props, ref) => null);
createSlots (function)Chunk child elements into buckets based on React components. Will also return the rest of the props.
Parameters:
InputProps) - The props to find the slots in. Either in props or childrenComponentMap) - A map of slot names to slot components(string | keyof InputProps)[]) - A list of props to omit from the final returned propsreturns: InputProps
const Example = props => {
const { header, body, footer, ...html } = createSlots(props, {
header: Header,
body: Body,
footer: Footer
});
return (
<div>
{header}
{body}
{footer}
</div>
);
};
// No matter what order given, it displays how we defined it!
const Usage = () => (
<Example>
<Footer>by me!</Footer>
<Body>Some Text</Body>
<Header>Title</Header>
</Example>
);
// or
const Usage = () => (
<Example>
<Footer>by me!</Footer>
<Body>Some Text</Body>
<Header>Title</Header>
</Example>
);
FocusLock (variable)Lock focus withing an area of the app
Portal (variable)Render an element inside of a portal.
const Example = () => <Portal>{'I am rendered at the end of the dom'}</Portal>;
DocGen (interface)Members:
{ description: string; }) - The generated docs for the react componentSlotted (interface)Members:
symbol) - The slot the styled element should render instyled (function)Create a react element with a className attached. The generated element accepts all the same props as the element prop.
Parameters:
T | [T, ...((props: any) => ReactNode)[]] | ((props: any) => ReactNode)) - The html dom element to create a Component forstring | WrappedComponent) - The class an metadata to attach to the Componentreturns: DocGen & Slotted & WithRef
const Wrapper = styled('div', {
class: styles.fancy,
description: 'A fancy component',
name: 'FancyWrapper'
});
const Example = ({ children, ...html }) => {
<Wrapper {...html}>{children}</Wrapper>;
};
Copyright 2013 - present © cnpmjs.org | Home |