Skip to content

chore: rename createStyles to createStyleSheet #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const App: React.FunctionComponent = () => (
)
```

**5. Access createStyles and useStyles with a factory**
**5. Access createStyleSheet and useStyles with a factory**

```ts
// styles.ts
Expand All @@ -119,22 +119,22 @@ import { breakpoints } from './breakpoints'
import { theme } from './theme'

export const {
createStyles,
createStyleSheet,
useStyles,
} = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
```

## Basic Usage

Library gives you two functions from the factory:
- `createStyles` which replaces `StyleSheet.create`
- `createStyleSheet` which replaces `StyleSheet.create`
- `useStyles` which parses your styles based on screen height, width and theme

```tsx
import React from 'react'
import { View, Text } from 'react-native'
// access createStyles and useStyles exported from factory
import { createStyles, useStyles } from 'lib/styles'
// access createStyleSheet and useStyles exported from factory
import { createStyleSheet, useStyles } from 'lib/styles'

export const ExampleUnistyles = () => {
const { styles } = useStyles(stylesheet)
Expand All @@ -148,7 +148,7 @@ export const ExampleUnistyles = () => {
)
}

const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
container: {
flex: 1,
justifyContent: 'center',
Expand All @@ -161,7 +161,7 @@ const stylesheet = createStyles(theme => ({
}))
```

`createStyles` takes an object like `StyleSheet.create` or function that injects your theme
`createStyleSheet` takes an object like `StyleSheet.create` or function that injects your theme

`useStyles` hook takes a `stylesheet` and returns an object with two keys:
- `styles` - parsed styles that can be used directly in React Native components
Expand All @@ -178,7 +178,7 @@ const { theme } = useStyles()
Any style can change based on breakpoints. To do this, change a value to an object:

```ts
const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
container: {
flex: 1,
justifyContent: 'center',
Expand Down Expand Up @@ -211,7 +211,7 @@ For more advanced usage and pixel perfect designs you can also use a custom medi
Media queries can be mixed with breakpoints, but have a bigger priority:

```tsx
const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
container: {
justifyContent: 'center',
alignItems: 'center',
Expand Down Expand Up @@ -255,7 +255,7 @@ export const ExampleUnistyles = () => {
)
}

const stylesheet = createStyles({
const stylesheet = createStyleSheet({
scrollContainer: {
flex: 1,
justifyContent: 'center',
Expand All @@ -272,11 +272,11 @@ const stylesheet = createStyles({

`react-native-unistyles` embraces the simplicity of `StyleSheet`, making it easy to integrate into your project.

You can replace `StyleSheet.create` with `createStyles` and it will work exactly the same:
You can replace `StyleSheet.create` with `createStyleSheet` and it will work exactly the same:

```diff
-const styles = StyleSheet.create({
+const styles = createStyles({
+const styles = createStyleSheet({
scrollContainer: {
flex: 1,
justifyContent: 'center',
Expand All @@ -299,8 +299,8 @@ With the hook in place, you can now use `breakpoints` and `media-queries`.
Additionally, to access the `theme` use a function instead of an `object`:

```diff
-const stylesheet = createStyles({
+const stylesheet = createStyles(theme => ({
-const stylesheet = createStyleSheet({
+const stylesheet = createStyleSheet(theme => ({
scrollContainer: {
flex: 1,
justifyContent: 'center',
Expand Down
4 changes: 2 additions & 2 deletions example/src/examples/Breakpoints.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Text, View } from 'react-native'
import { createStyles, useStyles } from '../styles'
import { createStyleSheet, useStyles } from '../styles'

// Use breakpoints for some values
export const Breakpoints: React.FunctionComponent = () => {
Expand All @@ -18,7 +18,7 @@ export const Breakpoints: React.FunctionComponent = () => {
)
}

const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
dynamicContainer: {
flex: 1,
justifyContent: 'center',
Expand Down
4 changes: 2 additions & 2 deletions example/src/examples/Extreme.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Text, View } from 'react-native'
import { createStyles, useStyles } from '../styles'
import { createStyleSheet, useStyles } from '../styles'

// Edge cases
export const Extreme: React.FunctionComponent = () => {
Expand All @@ -15,7 +15,7 @@ export const Extreme: React.FunctionComponent = () => {
)
}

const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
// dynamic function with hints
dynamicContainer: (flex: number) => ({
flex,
Expand Down
4 changes: 2 additions & 2 deletions example/src/examples/MediaQueries.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Text, View } from 'react-native'
import { createStyles, useStyles } from '../styles'
import { createStyleSheet, useStyles } from '../styles'

// Media queries
export const MediaQueries: React.FunctionComponent = () => {
Expand All @@ -18,7 +18,7 @@ export const MediaQueries: React.FunctionComponent = () => {
)
}

const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
dynamicContainer: {
flex: 1,
justifyContent: 'center',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import React from 'react'
import { Text, View } from 'react-native'
import { createStyles, useStyles } from '../styles'
import { createStyleSheet, useStyles } from '../styles'

// createStyles with StyleSheet.create compatible Object
export const MinimalWithCreateStyles: React.FunctionComponent = () => {
// createStyleSheet with StyleSheet.create compatible Object
export const MinimalWithCreateStyleSheet: React.FunctionComponent = () => {
const { styles } = useStyles(stylesheet)

return (
<View style={styles.container}>
<Text style={styles.text}>
I'm just a minimal example with createStyles
I'm just a minimal example with createStyleSheet
</Text>
</View>
)
}

const stylesheet = createStyles({
const stylesheet = createStyleSheet({
container: {
flex: 1,
justifyContent: 'center',
Expand Down
6 changes: 3 additions & 3 deletions example/src/examples/Theme.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import { Text, View } from 'react-native'
import { createStyles, useStyles } from '../styles'
import { createStyleSheet, useStyles } from '../styles'

// Injected theme to createStyles
// Injected theme to createStyleSheet
export const Theme: React.FunctionComponent = () => {
const { styles } = useStyles(stylesheet)

Expand All @@ -15,7 +15,7 @@ export const Theme: React.FunctionComponent = () => {
)
}

const stylesheet = createStyles(theme => ({
const stylesheet = createStyleSheet(theme => ({
container: {
flex: 1,
justifyContent: 'center',
Expand Down
2 changes: 1 addition & 1 deletion example/src/examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Minimal } from './Minimal'
export { MinimalWithCreateStyles } from './MinimalWithCreateStyles'
export { MinimalWithCreateStyleSheet } from './MinimalWithCreateStyleSheet'
export { Theme } from './Theme'
export { Breakpoints } from './Breakpoints'
export { MediaQueries } from './MediaQueries'
Expand Down
2 changes: 1 addition & 1 deletion example/src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createUnistyles } from 'react-native-unistyles'
import { breakpoints } from './breakpoints'
import { theme } from './theme'

export const { useStyles, createStyles } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
export const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)

export {
theme
Expand Down
10 changes: 10 additions & 0 deletions src/createUnistyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ export const createUnistyles = <B extends Record<string, number>, T = {}>(breakp
const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints)

return {
/**
* @deprecated The method should not be used, proposed version by the community is createStyleSheet, will be removed in RC
*/
createStyles: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
if (typeof styles === 'function') {
return styles as X
}

return styles as S
},
createStyleSheet: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
if (typeof styles === 'function') {
return styles as X
}

return styles as S
},
useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
const theme = useContext(UnistylesContext) as T
const dimensions = useDimensions()
Expand Down