1.2.9 • Published 2 months ago

@react-ck/react-utils v1.2.9

Weekly downloads
-
License
-
Repository
github
Last release
2 months ago

React CK | Utils Library

:warning: WARNING: This component library is being updated frequently and it's currently unstable due to being in it's early stages, we advice you to use only in production environments only after version 2.0.0.

Installation

To integrate the this library into your React apps, you can install it using npm or yarn: npm i --save @react-ck/react-utils or yarn add @react-ck/react-utils.

Utilities

Polimorphy

Component polimorphy allows consumer to choose which HTML tags and attributes will be rendered from a specific component.

Usage Examples

Implementation

import React, { isValidElement, useEffect, useMemo } from "react";
import styles from "./styles/index.module.scss";
import classNames from "classnames";
- import { getDisplayName } from "@react-ck/react-utils";
+ import {
+  getDisplayName,
+  PolymorphicComponent,
+  type ConsumerPolymorphicProps,
+  type HTMLTag,
+ } from "@react-ck/react-utils";

- export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
+ export interface ButtonProps<T extends HTMLTag>
+  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
+    ConsumerPolymorphicProps<T> {
  skin?: "primary" | "secondary" | "ghost" | "negative";
  size?: "s" | "m" | "l";
  icon?: React.ReactNode;
}

- export const Button = ({
+ export const Button = <T extends HTMLTag>({
+  as,
  skin = "primary",
  size = "m",
  icon,
  children,
  className,
  ...otherProps
- }: Readonly<ButtonProps>): React.ReactElement => {
+ }: Readonly<ButtonProps<T>>): React.ReactElement => {
  const isIconOnly = useMemo(
    () => icon !== undefined && React.Children.count(children) === 0,
    [children, icon],
  );

  return (
-    <button
-      className={classNames(
-        styles.root,
-        styles[skin],
-        styles[`size-${size}`],
-        {
-          [`${styles["icon-only"]}`]: isIconOnly,
-        },
-        className,
-      )}
-      {...otherProps}>
+    <PolymorphicComponent
+      as={as}
+      fallback={["button", otherProps]}
+      commonProps={{
+        className: classNames(
+          styles.root,
+          styles[skin],
+          styles[`size-${size}`],
+          {
+            [`${styles["icon-only"]}`]: isIconOnly,
+          },
+          className,
+        ),
+      }}>
      {icon && !isIconOnly ? <span className={styles.icon}>{icon}</span> : null}
      {children}
      {isIconOnly ? icon : null}
-     </button>
+    </PolymorphicComponent>
  );
};

Check the documentation website - react-ck.js.org.

1.2.9

2 months ago

1.2.8

2 months ago

1.2.7

2 months ago

1.2.6

2 months ago

1.2.5

2 months ago

1.2.4

2 months ago

1.2.3

2 months ago

1.2.2

2 months ago

1.2.1

2 months ago

1.2.0

3 months ago

1.1.1

4 months ago

1.1.0

5 months ago