0.8.5 • Published 9 months ago

xp.js-styled v0.8.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

xp.js(beta) is a framework for modern cross-platform React applications.

xp.js-styled is an single package from that framework.

The goal of this package is to build enterprise scalalable styled components with an easy-to-use minimal schema definition.

Key Features

  • CSS-in-JSX: Automatically generate CSS-in-JSX props for your components, making easy to style inline them without having to use the style prop directly.
  • Media Queries: Encourages devs to easily code responsive components by default, with a plain simple media queries API.
  • Deep Schema: All schemas can hold more schemas recursively, allowing to consider specific cases faster when using queries.
  • Color Shading: Create reshades of your colors with a simple lumen range syntax.
  • Variants Support: Included support for variants in the component schema.

Quick Start

The following guide will teach you how to get this working in your project rather fast!

CSS-in-JSX

The createStyled function returns a optimized high order wrapper around your component that applies the given style schema. Then the styled component subscribes the schema media queries to their specific device change listener, to optimally perform a style update.

This includes variants, prop changes and device changes.

For now, let's start creating a simple Styled View.

export const StyledView = createStyled(View);

StyledView now can be inline styled. Notice we pass backgroundColor, fontSize and fontWeight as plain props, and not as part of the style prop.

export default function Page() {
  return (
    <StyledView backgroundColor="nature.900" fontSize={18} fontWeight="bold">
      ...
    </StyledView>
  );
}

Media Queries

Exploring further into, of course you can create your styled components with a base style, using a schema that will be applied as the "global style" for that component, but with the option to iterate the styles using inline props.

For example, we conditionally apply a style override when the device is "medium-size". All devices that meet the query condition will apply the style.

export const StyledView = createStyled(View, {
  backgroundColor: "blueViolet.800",
  padding: 20,
  "@md": {
    backgroundColor: "blueViolet.700",
    padding: 25,
  },
});

For more details regarding the media queries included, the following tables contains the full list:

Color Scheme QueryDescription
@lightTargets devices or user settings that prefer a light color scheme.
@darkTargets devices or user settings that prefer a dark color scheme.

Platform QueryDescription
@iosTargets devices running the iOS operating system, such as iPhones and iPads.
@androidTargets devices running the Android operating system, such as Android phones and tablets.
@macosTargets devices running the macOS operating system, such as MacBooks and iMacs.
@windowsTargets devices running the Windows operating system, such as PCs and laptops.
@webTargets web-based platforms and browsers.

Size QueryDescription
@xxsTargets devices with very extra small screens, typically less than 360px in width.
@xsTargets devices with extra small screens, typically between 360px and 576px in width.
@smTargets devices with small screens, typically between 576px and 768px in width.
@mdTargets devices with medium screens, typically between 768px and 992px in width.
@lgTargets devices with large screens, typically between 992px and 1200px in width.
@xlTargets devices with extra large screens, typically between 1200px and 1600px in width.
@xxlTargets devices with very extra large screens, typically greater than 1600px in width.

Deep Nesting

When developing a style schema, you can implement any required media queries. However, for more precise control, especially when multiple conditions are involved, it's necessary to deep-nest your queries appropriately. This ensures styles are applied based on multiple conditions.

For example, in this code, the background color is "gray.500", but only is the device is larger, then we check if the device is an Android device, in which case the background color should be "blue.300." If the previous condition was met, but at the same time the color scheme is dark mode, then the background color should be "purple.200.", for iOS devices the sample logic will be used.

export const Box = createStyled(View, {
  backgroundColor: "gray.500",
  "@xl": {
    backgroundColor: "yellow.450",
    "@android": {
      backgroundColor: "blue.300",
      "@dark": {
        backgroundColor: "purple.200",
      },
    },
    "@ios": {
      backgroundColor: "green.300",
      "@dark": {
        backgroundColor: "yellow.200",
      },
    },
  },
});

Color System

The package comes with a set of built-in colors, to make easy prototyping

List of Built-in Colors

Color NameColor KeyColor Value
Redred#FF0000
Coralcoral#FF7F50
Orangeorange#FFA500
Goldgold#FFD700
Yellowyellow#FFFF00
Limelime#BCED0D
Light GreenlightGreen#78D61A
Greengreen#4EBF02
Dark GreendarkGreen#008000
Naturenature#008040
Tealteal#008080
Aquaaqua#009eb0
Skysky#00a2cf
Light BluelightBlue#007CCF
Blueblue#0057E3
Dark BluedarkBlue#002AE3
Blue VioletblueViolet#3900E3
Violetviolet#5A0EE8
Indigoindigo#6D04D6
Purplepurple#8500C7
Magentamagenta#A404BD
Pinkpink#BD04B7
Hot PinkhotPink#BD0461
Blackblack#000000
Graygray#808080
Whitewhite#FFFFFF

Theme Styling

Now, in case you need to share styles across multiple styled components, even with their responsiveness, then you can use a theme, and import the styles using the parentStyle prop.

export const Theme = createTheme({
  styles: {
    container: {
      padding: 5,
      margin: 5,
      "@md": {
        margin: 5,
        padding: 5,
      },
    },
    center: {
      alignItems: "center",
      justifyContent: "center",
    },
  },
});

export const Card = createStyled(View, {
  theme: Theme,
  parentStyles: ["container", "center"],
});

export const Banner = createStyled(View, {
  theme: Theme,
  backgroundColor: "yellow.400",
  parentStyles: ["container"],
});

Breakpoints

Define consistent spacing and sizing across your application using the built-in breakpoint system.

We provide for you common default to maintain a consistent and responsive design on your application, however you can customize those values in a theme. (see createTheme hook)

The default breakpoints that change to device sizes and viewport are:

BreakpointWidth
xxs>0px
xs>360px
sm>576px
md>768px
lg>992px
xl>1200px
xxl>1600px

Installation

Here you can copy/paste to install from usual package managers.

yarn add xp.js-state
npm install xp.js-state
pnpm add xp.js-state
jspm install npm:xp.js-state

Quick Demo

Here is a full example of how to use most of the package.

export const Card = createStyled(View, {
  backgroundColor: "gold.100",
  padding: 20,
  borderRadius: 10,
  shadowColor: "gray.850",
  shadowOffset: { width: 0, height: 2 },
  shadowOpacity: 0.8,
  shadowRadius: 2,
  elevation: 5,
  margin: 10,
});

export const StyledText = createStyled(Text);

export const StyledPressable = createStyled(Pressable, {
  backgroundColor: "indigo.600",
  padding: 10,
  borderRadius: 5,
  alignItems: "center",
  justifyContent: "center",
  marginTop: 20,
});

export default function Page(props) {
  return (
    <Card>
      <StyledText color="black" fontSize={18} fontWeight="bold">
        Card Title
      </StyledText>
      <StyledText color="black" marginTop={10}>
        This card component demonstrates how to use styled components with a color palette for consistent theming.
      </StyledText>
      <StyledPressable>
        <StyledText color="white.900">OK</StyledText>
      </StyledPressable>
    </Card>
  );
}
0.8.5

9 months ago

0.8.4

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago

0.8.3

9 months ago

0.8.2

9 months ago

0.7.4

10 months ago

0.7.3

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

0.4.1

1 year ago

0.3.13

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.3.9

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.2

1 year ago

0.3.0

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago