React-Native How to implement dark mode? - javascript

Hi there, I want to implement dark mode to my app. I have a file named ColorSchemes.js where is defined base theme and dark theme. So how could i implement theming baset on this file. I want to make save switching between themes at global level!
ColorSchemes.js
const darkTheme = {
main: palette.black,
background: palette.dark_grey,
alternative: palette.white_grey,
trackCardGradient: palette.black,
reviewCardGradient: palette.white_grey,
reviewCardTitle: palette.white_grey,
placeholderColor: palette.grey,
main_font: palette.light_grey,
second_font: palette.light_grey,
empty_star_color: palette.white_grey,
copy_right: palette.white,
search_bar: searchBarDarkTheme,
status_bar: statusBarDarkTheme,
};
const baseTheme = {
main: palette.blue,
background: palette.light_blue,
alternative: palette.white_blue,
trackCardGradient: palette.gradient_blue,
reviewCardGradient: palette.white_blue,
reviewCardTitle: palette.dark_blue,
placeholderColor: palette.midd_blue,
main_font: palette.light_blue,
second_font: palette.blue,
empty_star_color: palette.blue,
copy_right: palette.black,
search_bar: searchBarBaseTheme,
status_bar: statusBarBaseTheme,
};
// export const colors = darkTheme;
export const colors = baseTheme;

Create a Theme Manager in the form of React Context, that will provide app components the current theme, and a toggle() function to switch between themes

You can use the react-native-dark-mode library from below link. Also check out the example below.
React Native Dark Mode
App.js
import {initialMode, eventEmitter} from 'react-native-dark-mode';
state = {
mode: initialMode,
};
componentDidMount() {
eventEmitter.on('currentModeChanged', mode => {
this.setState({mode});
});
}
You can pass the state in your ScreenProps prop in your navigation AppContainer if you are using React Navigation.
ColorSchemes.js
import { DynamicValue } from 'react-native-dark-mode';
const colorSet = {
mainThemeBackgroundColor: new DynamicValue('#ffffff', '#000'),
mainThemeForegroundColor: new DynamicValue('#4991ec', '#4991ec'),
mainTextColor: new DynamicValue('#151723', '#ffffff'),
mainSubtextColor: new DynamicValue('#7e7e7e', '#f5f5f5'),
hairlineColor: new DynamicValue('#e0e0e0', '#222222'),
subHairlineColor: new DynamicValue('#f2f2f3', '#f2f2f3'),
tint: new DynamicValue('#3068CC', '#3068CC'),
grey: new DynamicValue('grey', 'grey'),
whiteSmoke: new DynamicValue('#f5f5f5', '#222222'),
headerStyleColor: new DynamicValue('#ffffff', '#222222'),
headerTintColor: new DynamicValue('#000000', '#ffffff'),
bottomStyleColor: new DynamicValue('#ffffff', '#222222'),
bottomTintColor: new DynamicValue('grey', 'lightgrey'),
mainButtonColor: new DynamicValue('#e8f1fd', '#062246'),
subButtonColor: new DynamicValue('#eaecf0', '#20242d'),
tabColor: new DynamicValue('#ffffff', '#121212'),
};
const navThemeConstants = {
light: {
backgroundColor: '#fff',
fontColor: '#000',
secondaryFontColor: '#7e7e7e',
activeTintColor: '#4991ec',
inactiveTintColor: '#ccc',
hairlineColor: '#e0e0e0',
},
dark: {
backgroundColor: '#121212',
fontColor: '#fff',
secondaryFontColor: '#fff',
activeTintColor: '#4991ec',
inactiveTintColor: '#888',
hairlineColor: '#222222',
},
main: '#4991ec',
};
This is an example on how you can use the react-native-dark-mode to get the dark mode working.

Related

how to put the CSS class name inside the MUI createTheme palette color name instead of the hardcode name

how to put the CSS class name inside the Material-UI(MUI) createTheme palette color name instead of the hardcode name.
const theme = createTheme({
palette: {
primary: {
light: "#66b53f",
main: **"I want to put css class name here"**,
dark: "#66b53f",
contrastText: "#fff",
},
secondary: {
light: "#ff7961",
main: "#f44336",
dark: "#ba000d",
contrastText: "#000",
},
},
});
IMO, A better place to define your custom classes inside your Theme is to define them within components/MuiCssBaseLine :
const theme = createTheme({
palette: {
primary: {
main: "#66b53f",
dark: "#66b53f",
},
secondary: {
main: "#f44336",
dark: "#ba000d",
},
},
components : {
MuiCssBaseLine : {
styleOverrides : {
body : {
"& .custom-css-class" : {
color : "white",
bgcolor : "grey",
... /* More stuffs */
}
}
}
}
}
});
then using className="custom-css-class" would be globally available to every child component wrapped within your ThemeProvider. You also need ,imported at top of your app right after Theme context.
import { CssBaseLine , ThemeProvider } from "#mui/material"
export default function App() {
/* Rest of your code */
return (
<ThemeProvider theme={theme}>
<CssBaseLine />
<YourApp />
<div className="custom-css-class">custom-styled-stuff</div>
</ThemeProvider>
);
}

Import classes Phaser 3

I have a problem cause I don’t know how it works.
I’ve created a scene where you can choose an upgrade for your character.
I have 3 upgrades you can choose from.
So I’m creating a class for each upgrade, I wanted to import them into the upgradeScene (UI).
I’ve tried this… but I can’t see anything with no errors in the console.
increase_speed.js
let rectC;
let cardC; //container
let increase_speed_title;
let increase_speed_description;
let increase_speed_btn;
export default class IncreaseSpeed extends Phaser.Scene {
constructor() {
super('increase_speed');
}
preload() {
this.load.image('cardbg', './Assets/UI/cardbg.png');
}
create() {
const screenCenterX = this.cameras.main.worldView.x + this.cameras.main.width / 2;
const screenCenterY = this.cameras.main.worldView.y + this.cameras.main.height / 2;
rectC = this.add.image(0, 0, 'cardbg');
increase_speed_title = this.add.text(-65, -100, 'Increase\nSpeed', {
fontFamily: 'dogicaPixel',
fontSize: '20px',
align: 'center'
});
increase_speed_description = this.add.text(-80, 0, 'Lorem ipsum\ndolor sit amet', {
fontFamily: 'dogicaPixel',
fontSize: '15px',
align: 'center'
});
increase_speed_btn = this.add.text(-65, 120, 'UPGRADE', {
fontFamily: 'dogicaPixel',
fontSize: '20px',
align: 'center'
});
cardC = this.add.container(screenCenterX, screenCenterY, [rectC, increase_speed_title, increase_speed_description, increase_speed_btn]);
}
}
upgradeScene.js in the create() function:
const increase_speed = new IncreaseSpeed(this, 300, 400);
this.add.existing(increase_speed);
I’m not sure if this is correct export default class IncreaseSpeed extends Phaser.Scene cause maybe should be a game object. But I have no idea how to do that.
If you are adding a scene (want it should stay a scene, and not use a different gameobject), you can simply call the function this.scene.add(...):
this.scene.add(this, IncreaseSpeed, true);
instead of calling:
const increase_speed = new IncreaseSpeed(this, 300, 400);
this.add.existing(increase_speed);
and the Scene should be displayed, as an overlay.
Here is a link to the documentation of this function.

How to set additional options for Chart.js BarCharts using React-Chartkick

I'm trying to display a barchart using React-Chartkick and Chart.js, and I'd like to customise the colours of the bars. Currently, I'm able to set all the bars to the same colour by passing a prop like this: <BarChart colours={["#fff"]} />.
Using LineCharts in React-Chartkick, you can set colours of the lines by passing an array of colours through that prop. BarCharts only seems to accept the first colour, however. Is this a limitation of React-Chartkick, or am I doing something wrong?
I've tried passing options (as described here: https://www.chartjs.org/docs/latest/charts/bar.html#styling ) through the library prop as that is how I've customised the colours of the axes and labels, but this doesn't seem to affect the bars.
Here's my current code:
state = {
chartLibraryOptions: {
borderColor: "#e34402", // does nothing here
backgroundColor: "#e34402", // nor this
scales: {
yAxes: [
{
ticks: { fontColor: "#fff", autoSkip: false }
}
],
xAxes: [
{
ticks: { fontColor: "#fff" }
}
]
}
}
};
render() {
return (
<BarChart
data={this.state.chartData}
library={this.state.chartLibraryOptions}
colors={["#e34402", "#e3b502"]} // All bars are the first colour
/>
);
}
I'm expecting to be able to change the colours of each bar, but after all this I'm not sure if that's possible through Chartkick?
Well, I used the same node package in a project with different approach kinda work for me. Almost all the charts take the same attributes.
Basically, this attribute dataset={{ backgroundColor: ['white', 'yellow' ], }}
is all you need to colour each bar. You can either pass string or array of string to backgroundColor.
The backgroundColor in dataset takes two types of data String and Array(object). Typical examples of passing data are below.
When you set backgroundColor to a string, it applied the same colour to each bar. e.g backgroundColor: 'red'
BarChart - <BarChart dataset={{ backgroundColor: 'red', }} />
When you set backgroundColor to an array, it applied each colour in the array to each bar. e.g backgroundColor: ['red', 'yellow'], then you create a loop of colours base on the data length.
column chart - <ColumnChart dataset={{ backgroundColor: ['red', 'yellow' ], }} />
React Implementation Below:
/* eslint-disable no-plusplus */
import React from 'react';
import { ColumnChart, BarChart } from 'react-chartkick';
import { chartOne } from '../common/chartData';
import 'chart.js';
const MonthlyGrowth = () => {
const handleBgColors = () => {
const firstColor = "#A00B16", secondColor = "#FAA226";
const arrOfBgColors = [];
for (let i = 1; i <= chartOne.length; i++) {
if (i % 2 === 0) {
arrOfBgColors.push(secondColor)
} else {arrOfBgColors.push(firstColor)}
}
return arrOfBgColors;
}
return (
<div className="bukka-card uk-card-default bg-white pt-4 pb-2 mr-1 pl-3 pr-2 pl-
lg-5">
<h2 className="mt-2">4,500,000</h2>
<BarChart
dataset={{ borderWidth: 0, width: 0, backgroundColor: handleBgColors(), }}
data={chartOne}
/>
</div>
)
}
export default MonthlyGrowth;

How to change size of Toggle Switch in Material UI

This is my first time using Material UI (I'm also a noob with react in general) and I cant seem to change the size of the toggle switch I'm using.
This is what I have so far -minus all the non related stuff:
import React, { Component } from "react";
import Switch from "#material-ui/core/Switch";
const styles = {
root: {
height: "500",
},
};
class ToggleActive extends Component {
state = {
checked: true,
};
handleChange = name => event => {
this.setState({ [name]: event.target.checked });
};
render() {
return (
<label htmlFor="normal-switch">
<Switch
classes={styles.root}
checked={this.state.checked}
onChange={this.handleChange("checked")}
/>
</label>
);
}
}
export default ToggleActive;
I just want to make it a bit larger, and change the color. Any help would be appreciated!
The change in the Switch component requires a little bit of detailed styling. I added some comments in parts that are not very obvious:
import {createStyles, makeStyles, Switch, Theme} from '#material-ui/core';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
width: 54,
height: 40,
padding: 0,
margin: theme.spacing(1),
},
switchBase: {
padding: 1,
'&$checked': {
// This is the part that animates the thumb when the switch is toggled (to the right)
transform: 'translateX(16px)',
// This is the thumb color
color: theme.palette.common.white,
'& + $track': {
// This is the track's background color (in this example, the iOS green)
backgroundColor: '#52d869',
opacity: 1,
border: 'none',
},
},
},
thumb: {
width: 36,
height: 36,
},
track: {
borderRadius: 19,
border: `1px solid ${theme.palette.grey[300]}`,
// This is the background color when the switch is off
backgroundColor: theme.palette.grey[200],
height: 30,
opacity: 1,
marginTop: 4,
transition: theme.transitions.create(['background-color', 'border']),
},
checked: {},
focusVisible: {},
})
);
You can implement this as a functional component:
import React, { useState } from 'react';
// import {createStyles, makeStyles, ...
// const useStyles = makeStyles((theme: Theme) => ...
export const ToggleItem: React.FC = () => {
const styles = useStyles();
const [toggle, setToggle] = useState<boolean>(false);
return (
<Switch
classes={{
root: styles.root,
switchBase: styles.switchBase,
thumb: styles.thumb,
track: styles.track,
checked: styles.checked,
}}
checked={toggle}
onChange={() => setToggle(!toggle)}
name={title}
inputProps={{'aria-label': 'my toggle'}}
/>
);
};
This is now even easier to accomplish because MUI has an official example in the documentation:
https://mui.com/material-ui/react-switch/#customization
Using that as an example, the minimum number of changes to accomplish making the switch bigger is actually just this:
export const MuiSwitchLarge = styled(Switch)(({ theme }) => ({
width: 68,
height: 34,
padding: 7,
"& .MuiSwitch-switchBase": {
margin: 1,
padding: 0,
transform: "translateX(6px)",
"&.Mui-checked": {
transform: "translateX(30px)",
},
},
"& .MuiSwitch-thumb": {
width: 32,
height: 32,
},
"& .MuiSwitch-track": {
borderRadius: 20 / 2,
},
}));
Here is the link to a forked sandbox with just a bigger switch:
https://codesandbox.io/s/customizedswitches-material-demo-forked-4m2t71
Consider this: I am not a front-end developer and did not develop in
Material-UI framework for years now. so just look for a different answer or send
me an edit version which works.
For changing the size of the switch component you should use size props that can be in two size 'small' || 'medium'.For example:
<Switch
size="small"
checked={this.state.checked}
onChange={this.handleChange("checked")}
color='primary'
/>
If it doesn't work for you then You need to change CSS style at root class:
const styles = {
root: {
height: 500,
width: 200},
};
Due to material-UI component API for changing the color of a switch you need to add a color props into you Switch JSX tag and choose from these enum:
enum: 'primary' |'secondary' | 'default'
your Switch should be like this:
<Switch
classes={styles.root}
checked={this.state.checked}
onChange={this.handleChange("checked")}
color='primary'
/>
Material-UI for switch size prop

Shoutem change default fontFamily not work

im newbie in react native! I'm used shoutem/ui in my project.
I have problem when modify default fontFamily. here is my code, please check then help me some solution to handle this.
thanks so much guys.
const theme = _.merge(getTheme(), {
defaultFont: {
fontFamily: 'Rubik-Italic',
},
'shoutem.ui.NavigationBar': {
'.clear': {
container: {
backgroundColor: params.primaryColor,
borderBottomColor: 'transparent',
position: 'relative',
},
'shoutem.ui.Title': {
color: 'white',
fontSize: 16,
},
},
'.normal': {
container: {
position: 'relative'
},
'shoutem.ui.Button': {
'shoutem.ui.Icon': {
color: params.colorTextAlpha,
},
'shoutem.ui.Text': {
color: params.colorTextAlpha,
},
},
'shoutem.ui.Title': {
color: params.colorText,
},
}
},
'shoutem.ui.Row': {
'.fix': {
paddingHorizontal: 10,
paddingVertical: 10,
}
}
});
What you want to do is override the theme variables.
Import the default theme variables and the getTheme function:
import {
getTheme,
defaultThemeVariables,
} from '#shoutem/ui';
Then define your custom variables:
const myThemeVariables = {
...defaultThemeVariables,
title: { fontFamily: 'MyFont' },
};
And then define your custom theme that uses those variables:
const myTheme = getTheme(myThemeVariables);
There is no more defaultFont setting you can override, so you'll have to be specific unfortunately.
Furthermore, you have to import the StyleProvider:
import { StyleProvider } from '#shoutem/theme';
And use it to control the style of your components:
render() {
return (
<StyleProvider style={myTheme}>
// your components here
</StyleProvider>
);
}
}

Categories