All Projects → ElForastero → pulsar-core

ElForastero / pulsar-core

Licence: other
🚀 Handy dynamic styles utilities for React Native and React Native Web.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to pulsar-core

pulsar-helm-chart
Apache Pulsar Helm chart
Stars: ✭ 27 (+0%)
Mutual labels:  pulsar
styled-variants
A scalable styled-component theming system that fully leverages JavaScript as a language for styles authoring and theming at both local and global levels.
Stars: ✭ 19 (-29.63%)
Mutual labels:  variants
react-native-web-playground
A React Native Web app with bunch of other stuffs and challenging UI's
Stars: ✭ 19 (-29.63%)
Mutual labels:  react-native-web
pulsar-client-node
Apache Pulsar NodeJS Client
Stars: ✭ 88 (+225.93%)
Mutual labels:  pulsar
HLA
xHLA: Fast and accurate HLA typing from short read sequence data
Stars: ✭ 84 (+211.11%)
Mutual labels:  variants
YAS
Yet Another Stylesheet (YAML-based Stylesheet Engine)
Stars: ✭ 23 (-14.81%)
Mutual labels:  stylesheet
pulsar-helm-chart
Official Apache Pulsar Helm Chart
Stars: ✭ 122 (+351.85%)
Mutual labels:  pulsar
aop
AMQP on Pulsar protocol handler
Stars: ✭ 93 (+244.44%)
Mutual labels:  pulsar
pulsar-beat-output
Elastic Beats Output to Apache Pulsar
Stars: ✭ 51 (+88.89%)
Mutual labels:  pulsar
recursive-variant
Recursive Variant: A simple library for Recursive Variant Types
Stars: ✭ 67 (+148.15%)
Mutual labels:  variants
Pulsar
Apache Pulsar - distributed pub-sub messaging system
Stars: ✭ 10,118 (+37374.07%)
Mutual labels:  pulsar
indigo
Indigo: SNV and InDel Discovery in Chromatogram traces obtained from Sanger sequencing of PCR products
Stars: ✭ 26 (-3.7%)
Mutual labels:  variants
styled-media-helper
💅 Helps manage media queries with styled components
Stars: ✭ 76 (+181.48%)
Mutual labels:  media-queries
pulsar-heartbeat
Pulsar Heartbeat monitors Pulsar cluster availability, tracks latency of Pulsar message pubsub, and reports failures of the Pulsar cluster. It produces synthetic workloads to measure end-to-end message pubsub latency.
Stars: ✭ 16 (-40.74%)
Mutual labels:  pulsar
rupture-sass
Better media queries mixins library for SASS
Stars: ✭ 47 (+74.07%)
Mutual labels:  media-queries
pdkvabs
pure data abstractions
Stars: ✭ 28 (+3.7%)
Mutual labels:  pulsar
rvtests
Rare variant test software for next generation sequencing data
Stars: ✭ 114 (+322.22%)
Mutual labels:  variants
tgip
TGIP (TGI Pulsar) is a weekly live video streaming about Apache Pulsar and its ecosystem.
Stars: ✭ 17 (-37.04%)
Mutual labels:  pulsar
react-native-web-expo-boilerplate
I am no longer in maintenance
Stars: ✭ 64 (+137.04%)
Mutual labels:  react-native-web
react-bones
💀 Dead simple content loading components for React and React-Native. 💀
Stars: ✭ 42 (+55.56%)
Mutual labels:  react-native-web

logo

@pulsar/core

npm License: MIT

Handy style utilities for React Native and React Native Web.

  • 📦 Lightweight (~2 KB)
  • 🚀 Fast (main work happens outside of component)
  • 👌 No dependencies
  • 👮‍♂️ Typed with TypeScript
  • ⚛️ Supports both Native and Web

What it looks like

import { DynamicStyleSheet, variants, maxWidth } from '@pulsar/core';

// Use `DynamicStyleSheet` in place of `StyleSheet`.
// It accepts a function whose first argument is a theme object,
// and returns styles as it does regular `StyleSheet`.
const useStyles = DynamicStyleSheet.create(theme => ({
  button: {
    borderRadius: theme.radii.ios,
    // You can define any component variations with `variants` helper.
    ...variants({
      primary: {
        backgroundColor: theme.colors.primary
      },
      secondary: {
        backgroundColor: theme.colors.secondary
      }
    }),
    // Media-queries can be used as well.
    ...maxWidth(theme.breakpoints.tablet, {
      height: 50
    })
  }
}));

const Button = ({ children, variant }) => {
  // `DynamicStyleSheet` returns a custom react hook.
  // It has optional parameter - props from which depend variants described above.
  const styles = useStyles({ variant });

  return (
    // styles.button here is an array of combined styles
    <View style={styles.button}>{children}</View>
  );
};

How is it different?

@pulsar/core doesn't call StyleSheet.create() during components rendering. All variants and media queries are flattened into main object and styles are created once during calling of DynamicStyleSheet.create().

The result looks like this:

// {
//   'button': {},
//   '_var:variant:primary:button': {},
//   '_var:variant:secondary:button': {},
//   '_media:max-width:768:button': {},
// }

Instead of calling StyleSheet.create() during rendering, the custom hook returned from DynamicStyleSheet.create() just manipulates with already existing and transpiled styles.

In the case above styles.button will contain an array of styles [styles['button'], styles['_var:variant:primary:button'], styles['_media:max-width:768:button']]. So you don't have to worry about merging all those keys together.

Installation

yarn add @pulsar/core
yarn add --dev babel-plugin-preval

Add babel-plugin-preval to your babel config. Please note that preval plugin should be listed first in plugins array (details):

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: ['preval'],
};

Configuring

  1. Import media-queries polyfill in the root of your app. Typically index.ts. It's required to emulate media queries in RN.
import '@pulsar/core/dist/polyfill';
  1. Overwrite Theme interface with your theme shape to enable properties validation and autocompletion. Create a .d.ts file, e.g. pulsar__core.d.ts.
// pulsar__core.d.ts

import '@pulsar/core';

declare module '@pulsar/core' {
  export interface Theme {
    // You can define any properties you want.
    breakpoints: {
      phone: 320,
      tablet: 768,
      desktop: 1280
    },
    colors: {
      primary: string,
      secondary: string,
    }
  }
}

Then define your light and dark themes using Theme interface.

// themes/themes.ts
import { Theme } from '@pulsar/core';

export const light: Theme = {
  breakpoints: {},
  colors: {}
}
  1. Wrap your app in <ThemeProvider />, passing light and dark themes as value. It's required to access the current theme via useTheme() hook.
import { ThemeProvider } from '@pulsar/core';
import { lightTheme, darkTheme } from './path/to/your/themes';

const pulsarConfig = {
  light: lightTheme,
  dark: darkTheme,
}

const App = () => (
  <ThemeProvider value={pulsarConfig}>
    {/* the rest of your app */}
  </ThemeProvider>
)
  1. Create .pulsar.config.js config file in the root of your project. This config should export light and dark themes and follow the following shape:
// .pulsar.config.js
module.exports = {
  themes: {
    light: { /* your theme definition here */ },
    dark: { /* your theme definition here */ },
  }
}

Or if you have your themes defined somewhere in src code, you can just re-export them to pulsar. E.g:

// .pulsar.config.js
const { light } = require('./src/themes/light');
const { dark } = require('./src/themes/dark');

module.exports = {
  themes: { light, dark },
};

Usage

Typical usage of @pulsar/core looks like this:

import { DynamicStyleSheet, variants } from '@pulsar/core';

const useStyles = DynamicStyleSheet.create(theme => ({
  button: {
    borderRadius: 8,
    ...variants({
      primary: {
        backgroundColor: theme.colors.primary
      },
      secondary: {
        backgroundColor: theme.colors.secondary
      }
    })
  },
}));

const Button = ({ children, variant }) => {
  const styles = useStyles({ variant });

  return (
    <View style={s.button}>{children}</View>
  )
};

Variants

Variants allow defining different component states dependent on its props.

import { DynamicStyleSheet, variants } from '@pulsar/core';

DynamicStyleSheet.create(theme => ({
  button: {
    // Define button size variants
    ...variants({
      prop: 'size',
      variants: {
        small: {
          height: 30,
        },
        normal: {
          height: 40,
        },
        large: {
          height: 50,
        },
      }
    }),
    // Shorthand for `prop = 'variant'`
    ...variants({
      primary: {
        color: theme.colors.primary,
      },
      secondary: {
        color: theme.cosors.secondary,
      },
      tertiary: {
        color: theme.cosors.tertiary,
      }
    }),
    // Arrays can be used as well
    ...variants([
      {
        prop: 'variant', variants: {}
      },
      {
        prop: 'size', variants: {}
      },
    ]),
  }
}));

Media Queries

Available media-queries:

  • minWidth
  • maxWidth
  • minHeight
  • maxHeight
import { DynamicStyleSheet, maxWidth } from '@pulsar/core';

DynamicStyleSheet.create(() => ({
  button: {
    alignSelf: 'flex-start',
    ...minWidth(768, {
      alignSelf: 'stretch'
    });
  }
}));
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].