Including scss in react-native project? - react-native

Is there a way to work with scss files in react native application? I am just curious and don't have much knowledge in react-native application architecture, But was thinking of using my current project scss files to work with the new react-native-android project we are planning to build.
REACT NATIVE VERSION : "0.46.4"
REACT VERSION : "16.0.0-alpha.12"
BABEL-PRESET-REACT-NATIVE: "2.1.0"
UPDATE :
I searched a little found out css-to-react-native which kinda let me transform my css into react-native stylesheet object, kinda workaround for me.
Thanks.

TL;DR: Yes, you can.
https://github.com/kristerkari/react-native-sass-transformer
It takes scss like:
.myClass {
color: blue;
}
.myOtherClass {
color: red;
}
and converts it to
var styles = {
myClass: {
color: "blue"
},
myOtherClass: {
color: "red"
}
};
Easy peasy.

TL;DR: No you cannot.
In react-native we are using inline style, which you can attain using Stylesheet.create.
Why you cannot CSS like styling is, In react native there are no #id either .class, so you cannot apply the style into #id or .class like you usually do in css styling. And also there is no related style, like in css you can do .element1 > .element2.
I think most developer main reason to use scss are to use nested class definition. Such as below:
.element1 {
> .element2 {
...
}
}
And because no class like feature in react-native component, there such method could not be apply in react-native.
Example how to create style in:
const styles = StyleSheet.create({
bigblue: {
color: 'blue',
fontWeight: 'bold',
fontSize: 30,
},
red: {
color: 'red',
},
});

Related

How to style nativewind element with multiple custom styles

I want to style a component with my own custom className that has certain properties. Tailwind lets you do this through App.css, but in a react native application there is none. How do you add say a '.title' class to tailwind with certain properties?
.title {
font-size: 24px
font-weight: 800
}
Applying it to Text JSX object:
<Text className="title">Title Text!</Text>
I figured out this method of styling multiple of the same components. Nativewind and Tailwind suggest not using this method, but it works for both.
In your tailwind.config.js file, add the following line of code above your module.exports:
const plugin = require("tailwindcss/plugin");
module.exports = {...}
Then in your plugins list, add:
plugin(function ({ addComponents }) {
addComponents({
".title": {
fontWeight: 800,
fontSize: 24,
},
});
}),
Make sure your styling uses the same property names as react-native, and NOT css (fontWeight vs font-weight). Restart your server, and it should work!
You can use twrnc npm package for this. Install it from here.
Import it like this in the file at the top.
import tw from "twrnc";
And use it like this
<Text style={tw` text-2xl font-extrabold`}>Title Text!</Text>

How to apply active-class prop in Vuetify component

Below was the old question, I realize the issue is not with the custom properties, but rather I have a component like the below, and 'active-class' is not being applied.
I tried to change the prop name to activeClass, doesn't seem to be working
<VListGroup
key={item.text}
class='drawer-item'
active-class='drawer-active-item'
>
I'm working on a new project using Vue, and trying to convert some old components (.vue) files to be using render functions and typescript (.tsx), and trying to separate the style contained in the <style/> blocks into a separate .less file.
While the template syntax, v-if and all can be converted quite easily, however I'm stuck on the part where if my style contains customProperties, https://vuetifyjs.com/en/features/theme/#custom-properties]]
specifically --v-primary-base, as the below
I have the below
.drawer-active-item {
color: white;
&::before {
background-color: var(--v-primary-base);
opacity: 1;
}
&:hover::before {
opacity: 0.8;
}
&::after {
background-color: var(--v-primary-base);
opacity: 1;
}
&:hover::after {
background-color: var(--v-primary-base);
opacity: 0.8;
}
}
Is there a way to access these custom css variables from the .less file? I've been googling things like 'access customProperties in less file` and things related, but I feel like perhaps I'm not asking the right question, or looking at the right resource for what I'm trying to achieve, pointers would be great, thank you.
I have a theme config that currently looks like the below, just for some context.
import Vue from 'vue';
import Vuetify from 'vuetify/lib';
import colors from 'vuetify/lib/util/colors';
Vue.use(Vuetify);
export default new Vuetify({
icons: {
iconfont: 'md',
},
theme: {
options: {
customProperties: true,
},
themes: {
light: {
primary: colors.blue.darken3,
},
dark: {
primary: colors.blue.darken3,
anchor: colors.blue.base,
},
},
},
});

Customize TextInput Label of the react-native-paper in the case of React Native Web

I'm working with the React Native Web and React Native Paper library with Styled Components. Basically I would like to customize the TextInput inner components: Label and html input
The questions are:
1) How to change Label styles? eg. width\size\color, etc. ?
2) How to change html input styles? I want to set outline: none to prevent the blue border show on focus in the case of browser.
I understand that in the case of native we don't have html and the native-web transpiles it.
And I can't understand how to catch the nested label component to change its styles. Because I want to show gray label when non-filled, violet when filled and the Input text should be black.
In the case of the web, it's trivial but in the case of native, I don't know how to handle it.
So is that possible at all?
Thanks for any help. Here is the code example
import React from 'react';
import {
View,
Platform,
} from 'react-native';
import {
TextInput as NativePaperInput,
withTheme,
} from 'react-native-paper';
import styled from 'styled-components/native';
const NativePaperInputThemed = withTheme(NativePaperInput);
export const TextInputStyled = styled(NativePaperInputThemed)`
${(props: any) => {
return `
outline: none; // doesn't work
input: { outline: none; } // doesn't work
& input: { outline: none; } // doesn't work
// Label change style ?
color: ${props.theme.theme10x.palette.typography.placeholder}; // doesn't work
border-color: '#f92a2a8a'; // doesn't work
height: 52px;
`;
}
}
`;
P.S. Basically even colors and fontFamily doesn't work somehow
label={<Text style={{fontSize: 20}}>{t('PersonalDetailsYuvitalOrg:editInput.firstName')}</Text>}
Some guy tried to change label style manually, the maintainer reponse:
You can pass fontSize via style prop. However it will affect both
label and input text. There is no way to change only one of them.
https://github.com/callstack/react-native-paper/issues/1505
You can pass a component for the label prop. For Example:
export const Input = styled(TextInput).attrs({
dense: true,
activeUnderlineColor: theme.colors.ui.blueDark,
label: <Text style={{fontSize: 50}}>My Custom Label</Text>
})`
width: 100%;
height: 50px;
font-size: 16px;
`
However, I have not looked for a way to control the scale when the animation occurs.

Is there a way of storing color hex codes in variables similar to SASS in React-Native?

I have colours in my React Native project and I was wondering if I can store the colours in variables similar to how you would using SASS - naming them and saving them in a separate file. Below is an example of my styles for some input fields.
module.exports = StyleSheet.create({
textInput:{
backgroundColor: '#fff',
color: '#666',
height: 60,
marginBottom: 20,
marginLeft: 20,
marginRight: 20,
paddingLeft: 10,
paddingRight: 10,
}
});
I want to know if I can do something similar to this (SASS):
$cornflower-blue: #6195ED; (with other colour variables in separate file)
article {
background-color: $cornflower-blue;
}
Thanks in advance.
You can literally use variables in JavaScript.
colors.js
export const cornFlowerBlue = '#6195ED';
when you want to use it
import {cornFlowerBlue} from './colors';
...
module.exports = StyleSheet.create({
article {
backgroundColor: cornFlowerBlue,
}
});
EDIT: $cornflower-blue will not work as a name because - is reserved for subtraction.
I can suggest you react-native-extended-stylesheet that supports global and local variables like SASS:
// app entry: set global variables
EStyleSheet.build({
textColor: '#0275d8'
});
// component: use global variables
const styles = EStyleSheet.create({
text: {
color: '$textColor'
}
});
I use a settings file where I import any constants across my app
'use strict';
var settings = {
siteURL: 'http://www.website.com',
green: '#80bc5a',
lightGreen: '#c0dead',
lightGrey: '#f1efeb',
darkGrey: '#54575a',
brown: '#867f75',
};
var settings = require('./Settings')
module.exports = settings;
var { siteURL, green } = settings

How to use Titanium app.tss in classic mobile app

I want to create a stylesheets for my Titanium app.
I'm not using Alloy framework, so how i can use app.tss? in which directory i need to store it?
Thank you
You can't.
.tss files are features bring by Alloy. However, if you want to use custom styles, you may have a look to the applyProperties method.
You can maybe do some workaround like this :
http://tifiddle.com/e9b87d0f3debd5e4a7bab3beb03dbddd
// Create window object
var win = Ti.UI.createWindow(),
label = Ti.UI.createLabel();
// Create or import properties
var properties = {
window: {
backgroundColor: "#1DB7FF"
},
label: {
text: "I Love Titanium",
color: "#FFFFFF"
}
};
// Apply properties
win.applyProperties(properties.window);
label.applyProperties(properties.label);
// Build views and launch
win.add(label);
win.open();
You can use CommonJS to achieve this
resources/styles.js
exports.Window = {
backgroundColor: 'blue',
backgroundImage: '/images/homebg.png',
exitOnClose: true
};
exports.Label = {
color: 'gray',
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
backgroundColor: 'transparent',
font: {
fontFamily:'Helvetica',
fontSize: '12dp',
fontStyle: 'normal',
fontWeight: 'normal'
}
};
Usages
var homeWin = Ti.UI.createWindow(require('styles').Window);
You can also refer this