0.1.73 • Published 2 years ago

styled-chroma v0.1.73

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

GitHub stars npm version npm bundle size License module formats

Documentation | API Reference | Contributing | Issues

Table of Contents

Installation

npm:

npm install styled-chroma

yarn:

yarn add styled-chroma

Features

  • Intuitive API for easy styling
  • Type-safe styling with TypeScript support
  • Performance optimized with minimal runtime overhead
  • Customizable and extendable
  • Automatic vendor prefixing for cross-browser compatibility
  • No dependencies on external libraries
  • Filters invalid props from being passed to the DOM
  • Supports dynamic styling based on props

Usage

Basic example:

import { styled } from "styled-chroma";

const Button = styled("button")`
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
`;

export default Button;

Using With Types

styled-chroma is fully compatible with TypeScript and provides type definitions for all HTML elements. Here's how you can use it with types:

  1. Basic usage with inferred types:
import { styled } from "styled-chroma";

const Button = styled("button")`
  background-color: blue;
  color: white;
  padding: 10px 20px;
`;

// Button will automatically have the correct HTML button element props
  1. Extending HTML element props:
import { styled } from "styled-chroma";

interface CustomButtonProps {
  isActive?: boolean;
}

const Button = styled<CustomButtonProps>("button")`
  background-color: ${(props) => (props.isActive ? "blue" : "gray")};
  color: white;
  padding: 10px 20px;
`;

// Usage
<Button isActive onClick={() => console.log("Clicked!")}>
  Click me
</Button>;
  1. Using with custom components:
import { styled } from "styled-chroma";
import { ComponentProps } from "react";

const CustomComponent = ({
  className,
  children,
}: {
  className?: string;
  children: React.ReactNode;
}) => <div className={className}>{children}</div>;

// Use ComponentProps to inherit props from the custom component.
// Only extend props when adding style to an existing component.
interface StyledCustomComponentProps
  extends ComponentProps<typeof CustomComponent> {
  backgroundColor?: string;
}

const StyledCustomComponent = styled<StyledCustomComponentProps>(
  CustomComponent
)`
  background-color: ${(props) => props.backgroundColor || "white"};
  padding: 20px;
`;

// Usage
<StyledCustomComponent backgroundColor="lightblue">
  Custom styled component
</StyledCustomComponent>;

By leveraging TypeScript with styled-chroma, you get full type checking for your styled components, including autocomplete for HTML attributes and custom props.

Exported Types

styled-chroma exports type definitions for all HTML elements. You can import these types from styled-chroma/dist/types. Here's a list of all exported types:

  • ButtonProps
  • AnchorProps
  • InputProps
  • TextareaProps
  • SelectProps
  • FormProps
  • ImageProps
  • DivProps
  • SpanProps
  • ParagraphProps
  • ListItemProps
  • UnorderedListProps
  • OrderedListProps
  • TableProps
  • TableRowProps
  • TableCellProps
  • HeaderProps
  • LabelProps
  • ArticleProps
  • SectionProps
  • NavProps
  • AsideProps
  • FooterProps
  • MainProps
  • AddressProps
  • AudioProps
  • VideoProps
  • CanvasProps
  • EmbedProps
  • IFrameProps
  • ObjectProps
  • PictureProps
  • SourceProps
  • TrackProps
  • DetailsProps
  • DialogProps
  • MenuProps
  • SummaryProps
  • DataProps
  • TimeProps
  • VarProps
  • CodeProps
  • PreProps
  • BlockquoteProps
  • CiteProps
  • DelProps
  • InsProps
  • KbdProps
  • MarkProps
  • QProps
  • SProps
  • SampProps
  • StrongProps
  • SubProps
  • SupProps
  • WbrProps
  • AreaProps
  • MapProps
  • ColProps
  • ColGroupProps
  • CaptionProps
  • THeadProps
  • TBodyProps
  • TFootProps
  • ThProps
  • FieldsetProps
  • LegendProps
  • DatalistProps
  • OptGroupProps
  • OptionProps
  • OutputProps
  • ProgressProps
  • MeterProps
  • HtmlProps
  • HeadProps
  • BaseProps
  • MetaProps
  • ScriptProps
  • NoScriptProps
  • TemplateProps
  • SlotProps

You can use these types to extend the props of your styled components or to type-check your components. For example:

import { styled, ButtonProps } from "styled-chroma";

interface CustomButtonProps extends ButtonProps {
  isActive?: boolean;
}

const Button = styled<CustomButtonProps>("button")`
  // ... styles ...
`;

This ensures type safety and provides autocompletion for all standard HTML attributes plus your custom props.

API Reference

styled

The styled function is the core function for creating styled components. It takes a tag name as an argument and returns a new component that applies the styles to the specified HTML element.

Parameters

  • tag: keyof JSX.IntrinsicElements - The HTML tag to be styled.

Returns

  • React.FC<any> - A new React functional component that renders the styled element.

styled('div'), styled('span'), etc.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for more details.

Issues

If you find any issues, please open an issue on the GitHub repository.

Contributing

We welcome contributions to styled-chroma! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

For more detailed information on how to contribute, please read our CONTRIBUTING.md guide.

Acknowledgments

0.1.71

2 years ago

0.1.72

2 years ago

0.1.73

2 years ago

0.1.69

2 years ago

0.1.68

2 years ago

0.1.67

2 years ago

0.1.66

2 years ago

0.1.65

2 years ago

0.1.64

2 years ago

0.1.63

2 years ago

0.1.61

2 years ago

0.1.60

2 years ago

0.1.59

2 years ago

0.1.58

2 years ago

0.1.57

2 years ago

0.1.56

2 years ago

0.1.55

2 years ago

0.1.54

2 years ago

0.1.53

2 years ago

0.1.52

2 years ago

0.1.51

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago