Coolshapes React

repository·main·Indexed 43 days ago

https://github.com/realvjy/coolshapes-react

A React component library for rendering over 100 abstract, high-quality SVG shapes featuring grainy mesh gradients. Version 2.0.0-beta.1 provides a Global component, category-specific components (e.g., Star, Ellipse), and utility functions like getRandomShape to integrate aesthetic abstract elements into UI/UX designs.

Tokens
8K
Snippets
40
Records
46
Agent score
56%

What's inside Coolshapes

  1. How to use Coolshapes components

    main

    Coolshapes provides two ways to render shapes: using a single Global component or using Category-specific components.

    1. Global Component

    Import the Coolshape component and specify the type (category) and index (specific shape within that category).

    2. Category Components

    Import specific components for a category (e.g., Star, Ellipse) and pass an index to select a specific shape within that category.

    // Global component example
    import { Coolshape } from "coolshapes-react";
    
    const App = () => {
      return <Coolshape type="star" index={1} size={48} noise={true} />;
    };
    
    // Category component example
    import { Star } from "coolshapes-react";
    
    const App = () => {
      return <Star index={1} size={48} />;
    };
  2. Generate random shapes

    main

    You can generate random shapes in two ways:

    1. Via Props: Set the random prop to true on a Coolshape component. If type is provided, it will pick a random shape from that category. If type is omitted, it picks a random category and a random shape.
    2. Via Function: Use the getRandomShape utility function.

    getRandomShape() returns a component. getRandomShape({ type: 'ellipse' }) returns a component from a specific category. getRandomShape({ onlyId: true }) returns a shape identifier object { shapeType, index } which can be passed directly as props to a component.

    import { Coolshape, getRandomShape } from "coolshapes-react";
    
    // Using props
    const Component = () => <Coolshape random={true} />;
    const Component2 = () => <Coolshape type="star" random={true} />;
    
    // Using function
    const randomComp = getRandomShape(); 
    const randomCompFromType = getRandomShape({ type: "ellipse" });
    const shapeId = getRandomShape({ onlyId: true, type: "star" }); // returns { shapeType: 'star', index: X }
  3. Styling Coolshapes with CSS

    main

    All components are assigned the default class name coolshapes. You can override this by passing a custom className prop. To make shapes responsive (similar to img elements), you can use CSS like this:

    .coolshapes {
      width: 100%;
      max-height: 48px;
    }
    import { Star } from "coolshapes-react";
    
    export const Shape = () => {
      return <Star className={\"myshape\"} />;
    };
  4. Use Coolshapes via UMD in the browser

    main

    If you are not using a bundler, you can use the UMD build via unpkg. The library is exposed on the window.coolshapes object.

    <!doctype html>
    <html
      >
      <head>
        <meta charset="UTF-8" />
        <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
        <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
        <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
        <script src="https://unpkg.com/coolshapes-react/dist/umd/coolshapes.js"></script>
      </head>
      <body
        >
        <div id="root"></div
        <script type="text/babel">
          const domContainer = document.querySelector("#root");
          const root = ReactDOM.createRoot(domContainer);
          const coolshapes = window.coolshapes;
          const Coolshape = coolshapes.Coolshape;
          root.render(<Coolshape />);
        </script>
      </body>
    </html>
  5. Reference shapeTypes and shapesCount

    main

    Use shapeTypes and shapesCount to programmatically access the available shape categories and the number of shapes in each category.

    • shapeTypes: An array of category names (e.g., ['star', 'ellipse', ...]).
    • shapesCount: An object mapping category names to their respective counts (e.g., { star: 13, ellipse: 12, ... }).
    import { shapeTypes, shapesCount } from "coolshapes-react";
    // shapeTypes: ['star', 'ellipse', ...]
    // shapesCount: { star: 13, ellipse: 12, ... }
  6. Coolshape component props

    main

    The following props can be passed to Coolshape or category-specific components to customize the appearance:

    namedata typedefaultdescription
    sizeNumber200The dimension of shape
    typeStringrandomThe category of shapes (e.g., star, ellipse). If empty, selects a random category.
    noiseBooleantrueWhether to add noise to the shape
    indexNumberrandomThe index of shape within the category. Note: number category uses 0-9, others start from 1.
    randomBooleanfalseIf true, selects a random component
    fillStringnoneOverrides mesh gradient with a flat hex/rgba color (e.g., "#000000"). Use "transparent" to remove color.
    gradientStringdefaultApplies a preset gradient (e.g., "gradient-1"). Set to false or leave empty when using fill.
    outlineNumber0Adds a stroke outline of the specified width
    outlineColorString#fffThe color of the outline stroke
    outlineJoinStringbevelSVG stroke-linejoin (round, miter, bevel)
    outlineCapStringroundSVG stroke-linecap (round, butt, square)
    blurNumber0Applies a Gaussian blur filter of the specified pixel radius
    transparentBooleanfalseRemoves the underlying white background layer

    Note: Since these are SVG components, they also support all standard SVG element props.

  7. Shape categories

    main

    Coolshapes contains 115 shapes across the following categories. When passing a category name to the type prop, use lowercase names:

    • star (13 shapes)
    • triangle (14 shapes)
    • moon (15 shapes)
    • polygon (8 shapes)
    • flower (16 shapes)
    • rectangle (9 shapes)
    • ellipse (12 shapes)
    • wheel (7 shapes)
    • misc (11 shapes)
    • number (10 shapes - maps to digits 0-9)
  8. Access gradient presets via named exports

    main

    The library provides a large collection of pre-defined gradient presets. You can import these presets individually by their specific names (e.g., Gradient1, Gradient2, etc.) from the src/gradients entrypoint.

    import { Gradient1, Gradient2 } from './src/gradients';
    
    // Use the imported gradients in your components
  9. Use the Wheel component

    main

    The Wheel component is a composite component that renders different variations of wheel shapes based on the provided shape data. It is exported as both the default export and a named export. The component is dynamically resolved using the getComponentWithShapeType utility, which maps specific shape data to their corresponding React components.

    import Wheel from './shapes/wheel';
    
    // Use the default export
    <Wheel />
    
    // Or use the named export
    import { Wheel } from './shapes/wheel';
    <Wheel />
  10. Use the CoolShape component

    main

    The CoolShape component is the primary entrypoint for rendering shapes. It is exported from the main package and serves as the core component for the library.

    import { CoolShape } from 'coolshapes-react';
    
    // Usage example (implied by export * from './CoolShape')
    <CoolShape />