Skip to content

Commit

Permalink
Merge pull request #42 from jpudysz/2.0
Browse files Browse the repository at this point in the history
v.2.0
  • Loading branch information
jpudysz authored Dec 13, 2023
2 parents d581394 + ea83471 commit d3aea03
Show file tree
Hide file tree
Showing 231 changed files with 16,155 additions and 6,693 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true

[*.{sh,json,podspec}]
[*.{sh,json,podspec,yml,yaml}]
indent_style = space
indent_size = 2
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": 0,
"@typescript-eslint/ban-types": 0,
"@typescript-eslint/no-empty-interface": 0,
"no-duplicate-imports": 0,
"functional/immutable-data": 0
"functional/immutable-data": 0,
"no-underscore-dangle": 0
}
}
32 changes: 27 additions & 5 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ body:
value: |
Thanks for taking the time to fill out this bug report!
Before submitting a new bug/issue, please check the links below to see if there is a solution or question posted there already:
- Issues tab: https://github.com/jpudysz/react-native-unistyles/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc
- Closed issues tab: https://github.com/jpudysz/react-native-unistyles/issues?q=is%3Aissue+is%3Aclosed
- Discussions tab: https://github.com/jpudysz/react-native-unistyles/discussions
Before submitting a new bug/issue, please check the links below to see if there is a solution or question posted there already:
- Issues tab: https://github.com/jpudysz/react-native-unistyles/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc
- Closed issues tab: https://github.com/jpudysz/react-native-unistyles/issues?q=is%3Aissue+is%3Aclosed
- Discussions tab: https://github.com/jpudysz/react-native-unistyles/discussions
- type: markdown
attributes:
value: |
Expand Down Expand Up @@ -50,7 +50,7 @@ body:
attributes:
label: Unistyles version
description: What version of react-native-unistyles are you using?
placeholder: 1.0.0
placeholder: 2.0.0
validations:
required: true

Expand All @@ -73,5 +73,27 @@ body:
- Android
- iOS
- React Native Web
- SSR
- React Native Windows

- type: dropdown
id: engine
attributes:
label: Engine
description: Which engine do you use?
multiple: false
options:
- Hermes
- JSC

- type: dropdown
id: architecture
attributes:
label: Architecture
description: Which architecture do you use?
multiple: false
options:
- Paper (old)
- Fabric (new)
validations:
required: true
2 changes: 0 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. -->

## Summary

<!-- Explain the motivation for this PR. Include "Fixes #<number>" if applicable. -->
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
fetch-depth: 0

- name: Setup
uses: ./.github/actions/setup
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ android.iml

# Cocoapods
#
examples/expo/ios/Pods
examples/expo/ios
examples/expo/android

# Ruby
examples/expo/vendor/
Expand Down Expand Up @@ -81,3 +82,4 @@ example/expo/.yarn
#docs
docs/.yarn
coverage
docs/dist
57 changes: 22 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,42 @@
[<img alt="react-native-unistyles" src="assets/banner.png">](https://codemask.com)


<picture>
<source media="(prefers-color-scheme: dark)" srcset="assets/uni-dark.svg">
<img alt="react-native-unistyles" src="assets/uni-light.svg">
</picture>
[<img alt="react-native-unistyles" src="assets/banner.png">](https://reactnativeunistyles.vercel.app/)

![GitHub package.json version](https://img.shields.io/github/package-json/v/jpudysz/react-native-unistyles?style=for-the-badge)
[![npm downloads](https://img.shields.io/npm/dm/react-native-unistyles.svg?style=for-the-badge)](https://www.npmjs.com/package/react-native-unistyles)
![Platform - Android and iOS](https://img.shields.io/badge/platform-Android%20%7C%20IOS%20%7C%20SSR%20%7C%20Web-blue.svg?style=for-the-badge)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?style=for-the-badge)](https://opensource.org/licenses/MIT)

## [Documentation](https://reactnativeunistyles.vercel.app/)
- [Start here](https://reactnativeunistyles.vercel.app/start/introduction/)
- [API](https://reactnativeunistyles.vercel.app/reference/create-stylesheet/)
- [Show case](https://reactnativeunistyles.vercel.app/show-case/projects/)
- [Examples](https://reactnativeunistyles.vercel.app/examples/all/)

## Features
- ⚡ Blazing fast, adds around ~3ms on top of StyleSheet*
- 🚀 Shared core with C++ and JSI bindings
- 🌉 Supports new architecture
- 🔥 Crazy performance, adds under 0.1 ms to your StyleSheet
- 🎳 Share up to 100% of your styles across platforms in monorepo
- 🎯 Doesn't introduce new components
- 🖥️ Supports custom breakpoints and css-like media queries
- 🎨 Access theme in your StyleSheets and components
- 🪄 Supports dynamic functions to access values from JSX
- 🥳 Compatible with Expo, Expo Go, Bare React Native, React Native Web and SSR
- 🎯 Doesn't introduce new components, everything is packed in one hook
- ⚛️ No React Context
- 🖥️ Supports custom breakpoints, css-like media queries and variants
- 🎨 Register multiple themes and change them with single function call
- 🥳 Compatible with Expo, Bare React Native, React Native Web and SSR
- 🛡️ ~99% Test coverage
- 🔌 Extend stylesheets with your own plugins
- ⚔️ No 3rd party dependencies

*-based on this [benchmark](https://github.com/efstathiosntonas/react-native-style-libraries-benchmark)
- and much much more!

## Installation

```cmd
yarn add react-native-unistyles
```

## [Documentation](https://reactnativeunistyles.vercel.app/)
- [Start here](https://reactnativeunistyles.vercel.app/start/setup/)
- [References](https://reactnativeunistyles.vercel.app/reference/create-stylesheet/)
- [Examples](https://reactnativeunistyles.vercel.app/example/breakpoints/)

## Faster, better and simpler - v.2.0 🚀

There is ongoing work on branch [2.0](https://github.com/jpudysz/react-native-unistyles/tree/2.0).

---

Join early testers and discussion [here](https://github.com/jpudysz/react-native-unistyles/discussions/41).

```cmd
```shell
# Unistyles is currently in RC phase
yarn add react-native-unistyles@next
```

## Blog post
## Discord
Looking for help or you want to chat with me?

Read about what drove me to create this library in this blog post [here](https://www.reactnativecrossroads.com/posts/level-up-react-native-styles).
[Join Discord](https://discord.gg/akGHf27P4C)

## Sponsor my work

Expand Down
27 changes: 27 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.9.0)

project(unistyles)

add_library(unistyles
SHARED
../cxx/UnistylesRuntime.cpp
./src/main/cxx/cpp-adapter.cpp
)

include_directories(
../cxx
)

set_target_properties(unistyles PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

find_package(ReactAndroid REQUIRED CONFIG)

target_link_libraries(unistyles
ReactAndroid::jsi
android
)
75 changes: 75 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
buildscript {
ext.safeExtGet = {prop, fallback ->
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
repositories {
google()
gradlePluginPortal()
}
dependencies {
classpath("com.android.tools.build:gradle:7.3.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.22")
}
}

apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.android'

def resolveBuildType() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()

return tskReqStr.contains('Release') ? 'release' : 'debug'
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 33)
namespace "com.unistyles"

defaultConfig {
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
minSdkVersion safeExtGet('minSdkVersion', 21)
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
}
}
}

buildFeatures {
prefab true
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

packagingOptions {
doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
excludes = [
"META-INF",
"META-INF/**",
"**/libjsi.so",
"**/libc++_shared.so"
]
}
}

repositories {
mavenCentral()
google()
}

dependencies {
implementation 'com.facebook.react:react-native'
}
108 changes: 108 additions & 0 deletions android/src/main/cxx/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <jni.h>
#include <jsi/jsi.h>
#include "UnistylesRuntime.h"

using namespace facebook;

static jobject unistylesModule = nullptr;
std::shared_ptr<UnistylesRuntime> unistylesRuntime = nullptr;

void throwKotlinException(
JNIEnv *env,
const char *message
) {
jclass runtimeExceptionClass = env->FindClass("java/lang/RuntimeException");

if (runtimeExceptionClass != nullptr) {
env->ThrowNew(runtimeExceptionClass, message);
env->DeleteLocalRef(runtimeExceptionClass);
}
}

extern "C"
JNIEXPORT void JNICALL
Java_com_unistyles_UnistylesModule_nativeInstall(
JNIEnv *env,
jobject thiz,
jlong jsi,
jint screenWidth,
jint screenHeight,
jstring colorScheme
) {
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);

if (unistylesModule == nullptr) {
unistylesModule = env->NewGlobalRef(thiz);
}

if (runtime == nullptr || unistylesModule == nullptr) {
return throwKotlinException(env, "Something went wrong while initializing UnistylesModule");
}

const char *colorSchemeChars = env->GetStringUTFChars(colorScheme, nullptr);
std::string colorSchemeStr(colorSchemeChars);
env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);

unistylesRuntime = std::make_shared<UnistylesRuntime>(
screenWidth,
screenHeight,
colorSchemeStr
);

unistylesRuntime->onThemeChange([=](const std::string &theme) {
jstring themeStr = env->NewStringUTF(theme.c_str());
jclass cls = env->GetObjectClass(unistylesModule);
jmethodID methodId = env->GetMethodID(cls, "onThemeChange", "(Ljava/lang/String;)V");

env->CallVoidMethod(unistylesModule, methodId, themeStr);
env->DeleteLocalRef(themeStr);
env->DeleteLocalRef(cls);
});

unistylesRuntime->onLayoutChange([=](const std::string &breakpoint, const std::string &orientation, int width, int height) {
jstring breakpointStr = env->NewStringUTF(breakpoint.c_str());
jstring orientationStr = env->NewStringUTF(orientation.c_str());
jclass cls = env->GetObjectClass(unistylesModule);
jmethodID methodId = env->GetMethodID(cls, "onLayoutChange", "(Ljava/lang/String;Ljava/lang/String;II)V");

env->CallVoidMethod(unistylesModule, methodId, breakpointStr, orientationStr, width, height);
env->DeleteLocalRef(breakpointStr);
env->DeleteLocalRef(orientationStr);
env->DeleteLocalRef(cls);
});

unistylesRuntime->onPluginChange([=]() {
jclass cls = env->GetObjectClass(unistylesModule);
jmethodID methodId = env->GetMethodID(cls, "onPluginChange", "()V");

env->CallVoidMethod(unistylesModule, methodId);
env->DeleteLocalRef(cls);
});

jsi::Object hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);

runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
}

extern "C"
JNIEXPORT void JNICALL
Java_com_unistyles_UnistylesModule_nativeDestroy(JNIEnv *env, jobject thiz) {
unistylesRuntime.reset();
unistylesModule = nullptr;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_unistyles_UnistylesModule_nativeOnOrientationChange(JNIEnv *env, jobject thiz, jint width, jint height) {
if (unistylesRuntime != nullptr) {
unistylesRuntime->handleScreenSizeChange(width, height);
}
}

extern "C"
JNIEXPORT void JNICALL
Java_com_unistyles_UnistylesModule_nativeOnAppearanceChange(JNIEnv *env, jobject thiz, jstring colorScheme) {
if (unistylesRuntime != nullptr) {
unistylesRuntime->handleAppearanceChange(env->GetStringUTFChars(colorScheme, nullptr));
}
}
Loading

0 comments on commit d3aea03

Please sign in to comment.