0.0.83 • Published 2 years ago

pu2 v0.0.83

Weekly downloads
50
License
MIT
Repository
github
Last release
2 years ago

pu2

Build Status NPM version Standard Version


Me

Style Lib

Everything goes through <Box>

// src/client/MyButton.tsx
import React from 'react';
import { Box } from 'pu2';


// Renders a <div> element with a button role
export const MyButton = ({ children, onClick }) => (
	<Box
		role="button"
		cursor="pointer"
		display="flex"
		alignItems="center"
		justifyContent="center"
		onClick={onClick}
	>
		{children}
	</Box>
);

// Renders a <button> element with some icon
export const MyButton2 = ({ children, icon, onClick }) => (
	<Box as="button" onClick={onClick}>
		{icon}
		{children}
	</Box>
);

// Passing some css as a prop will override style props (renders a black button with white text)
const css = { color: 'white', background: 'black' };
export const MyButton4 = ({ children, onClick }) => (
	<Box color="orange" css={css} onClick={onClick}>
		{children}
	</Box>
);

// Get fancy. Generate classnames on-the-fly, including media queries and sub-selectors.
const css = {
	color: 'lightblue',
	
	// Match descendent children (notice the space before the selector)
	' span': {
		color: 'green',

		'[data-value]': { color: 'silver' }, // Match attribute

		'.x': { color: 'wheat' } // Match classname (no space before the selector)
	},
	' .b': { color: 'orange' },

	// Match immediate children
	'>span': { color: 'blue' },
	'>.b': {
		color: 'yellow',

		':hover': { color: 'cyan' } // Match element state
	},

	// Match a media query
	'@media screen and (max-width: 600px)': {
		color: 'magenta',

		'>span': { color: 'red' }
	}
}
export const MyButton5 = ({ onClick, prefix }) => (
	<Box css={css} onClick={onClick}>
		lightblue...or magenta when <=600px screen width
		<Box className="a">black</Box>
		<Box as="span">
			blue...or red when <=600px screen width
			<Box className="b">orange</Box>
		</Box>
		<Box className="b">
			yellow...or cyan when hovering
			<Box as="span">green</Box>
			<Box as="span" data-value="10">silver</Box>
			<Box as="span" className="x">wheat</Box>
		</Box>
		<Box className="b">
	</Box>
)

Basic SSR

// src/client/App.ts
import React from 'react';
import { Box } from 'pu2/style-lib';

export const App = () => (
	<Box background="blue" p="16px">
		<Box color="red" background="white" p="8px">
			Text
		</Box>
	</Box>
);
// src/client/main.client.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserStyleProvider } from 'pu2/style-lib';
import { App } from './App';

ReactDOM.hydrate(
	<BrowserStyleProvider>
		<App />
	</BrowserStyleProvider>,
	document.body
)
// src/server/main.server.ts
import React from 'react';
import express from 'express';
import { renderToString } from 'react-dom/server';
import { SSRStyleProvider, SSRStyleCollector } from 'pu2/style-lib/server';
import { App } from '../client/App';

const renderSSR = ({ appHtml, styleHtml }) => `
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>My App</title>
		${styleHtml}
	</head>
	<body>
		${appHtml}
	</body>
</html>
`;

const server = express();

server.get('/', async (req, res) => {
	const collector = new SSRStyleCollector();
	const appHtml = renderToString(
		<SSRStyleProvider collector={collector}>
			<App />
		</SSRStyleProvider>
	);
	const styleHtml = collector.getHtml();

	res.status(200)
		.set({ 'Content-Type': 'text/html' })
		.end(renderSSR({ appHtml, styleHtml }));
});
server.listen(3000);
0.0.80

2 years ago

0.0.81

2 years ago

0.0.82

2 years ago

0.0.83

2 years ago

0.0.73

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.79

2 years ago

0.0.70

2 years ago

0.0.71

2 years ago

0.0.72

2 years ago

0.0.62

2 years ago

0.0.63

2 years ago

0.0.64

2 years ago

0.0.65

2 years ago

0.0.66

2 years ago

0.0.67

2 years ago

0.0.68

2 years ago

0.0.69

2 years ago

0.0.61

2 years ago

0.0.59

2 years ago

0.0.52

2 years ago

0.0.53

2 years ago

0.0.54

2 years ago

0.0.55

2 years ago

0.0.56

2 years ago

0.0.58

2 years ago

0.0.51

3 years ago

0.0.50

3 years ago

0.0.48

3 years ago

0.0.49

3 years ago

0.0.47

3 years ago

0.0.46

3 years ago

0.0.45

3 years ago

0.0.43

3 years ago

0.0.44

3 years ago

0.0.40

3 years ago

0.0.41

3 years ago

0.0.42

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.37

4 years ago

0.0.35

4 years ago

0.0.36

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.22

4 years ago

0.0.24

4 years ago

0.0.25

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago