3.0.354 • Published 1 day ago

easy-with-style v3.0.354

Weekly downloads
3
License
MIT, Anti-996
Repository
github
Last release
1 day ago

Easy with Style

Programmatic styles for Easy.

To find out what variant of CSS is supported, which is essential to know, please read the With Style readme file.

Installation

With npm:

npm install easy-with-style

You can also clone the repository with Git...

git clone https://github.com/djalbat/easy-with-style.git

...then install the dependencies with npm from within the project's root directory:

npm install

You can also run a development server, see the section on building later on.

Example

There is a small development server that can be run from within the project's directory with the following command:

npm start

The example will then be available at the following URL:

http://localhost:8888

The source for the example can be found in the src/example.js file and corresponding src/example folder. You are encouraged to try the example whilst reading what follows. You can rebuild it on the fly with the following command:

npm run watch-debug

The development server will reload the page whenever you make changes.

One last thing to bear in mind is that this package is included by way of a relative rather than a package import. If you are importing it into your own application, however, you should use the standard package import.

Usage

You must call the renderStyles() function after importing the view but before rendering it. Doing so ensures that the styles generated as a result of executing the view code are inserted into the DOM before the view itself.

import withStyle from 'easy-with-style';

import { Body } from 'easy';

import View from './view';

const { renderStyles } = withStyle;

const body = new Body();

renderStyles();

body.mount(

  <View />

);

Note that rendering the styles in this way is not done as part of the build process, you must explicitly call the renderStyles() function, ideally right before you attach the view to the body.

Creating primitive elements with style

All of the standard HTML elements are supported. For a complete list of tag names, see the tagNames.js file. You can create these elements, which are functional elements under the hood, as follows:

const Link = withStyle.a`

  color: ${white};
  text-decoration: none;

  @media (min-width: ${desktop}) {
    color: ${black};
  }

`;

Now you are free to use the Link element in the usual way. Note that expression interpolation is supported. For example, here colour and breakpoint variables have been used.

To learn more about template literals in general and expression interpolation in particular, see the relevant MDN page.

Creating functional elements with style

This can be done with the withStyle() function:

const Header = (properties) => {
  const { className } = properties;

  return (

    <header className={className}>

      ...

    </header>

  );
};

export default withStyle(Header)`

  ...

`;

Note that the className property is retrieved from the properties object and must be used as the value of the attribute of the same name on the outermost JSX element that the function returns.

Creating class elements with style

Creating your own elements by extending the Element or InputElement class, or any supported element class for that matter, is also relatively straightforward:

import { Element } from "easy";

class Div extends Element {
  static tagName = "div";

  ...

  static fromProperties(Class, properties) {
    if (properties === undefined) {
      properties = Class; ///

      Class = Div;
    }

    return Element.fromProperties(Div, properties);
  }
}

export default withStyle(Div)`

  ...

`;

The one caveat to be aware of is that the static fromProperties() factory method must be polymorphic as shown. This is because the anonymous class that the withStyle() method returns calls the fromProperties() method and expects this signature.

Extending the styles of elements with style

If all you want to do is to add further styles to an element, be it a primitive, functional or class element, simply wrap it in another withStyle() call:

const HeaderLink = withStyle(Link)`

  ...

`;

In this case the Link element will keep its own styles whilst the HeaderLink element will both inherit those styles and of course possess its own.

Elements with style and composition

Composing elements with style obviously causes no problems in general, aside from one small caveat. If you set the className property of an element with style, then you will overwrite the class name that has been given to it automatically. In the case of all functional and class elements with style, however, it is easy to recover the class name and incorporate it into your own:

const NavigationButton = (properties) => {
  const { className } = Button,
        { children } = properties;

  return (

    <Button className={`${className} navigation`}>
      {children}
    </Button>

  );
}

This situation occasionally arises when using placeholder class names, see below.

Class elements with style and class inheritance

Extending your own class elements with style involves nothing more than remembering the correct signature for the static fromProperties() factory method, there is nothing more to do:

class MainDiv extends Div {
  ...

  static defaultProperties = {
    className: "main"
  };

  static fromProperties(Class, properties) {
    if (properties === undefined) {
      properties = Class; ///

      Class = MainDiv;
    }

    return Div.fromProperties(MainDiv, properties);
  }
}

export default withStyle(MainDiv)`

  ...

`;

Note that the element has been given a class name by way of the static defaultProperties class field in the normal fashion. The automatically generated class name for the styles does not interfere with this process. Again this is an example of placeholder class names, see immediately below.

Placeholder class names

Class names are randomly generated hashes of around eight characters, and as such are far from ideal when debugging. It is best to add your own placeholder class names, therefore. For functional components the following pattern is recommended:

const MainHeader = (properties) => {
  const { className } = properties;

  return (

    <header className={`${className} main`}>

      ...

    </header>

  );
};

export default withStyle(MainHeader)`

  ...

`;

For class components there is nothing to do, just add your own placeholder class name by wa of the className property of the defaultProperties static class field, as above.

Placeholder class names make the association of DOM elements in your browser's developer tools with their corresponding components far easier.

An example of functional classes

Elements with style are great for working with styles relating to an element's functionality as opposed to just its appearance.

class Div extends Element {
  hide() {
    this.addClass('hidden');
  }

  display() {
    this.removeClass('hidden');
  }

  isHidden() {
    const hidden = this.hasClass('hidden');

    return hidden;
  }

  isDisplayed() {
    const hidden = this.isHidden(),
          displayed = !hidden;

    return displayed;
  }

  ...
}

export default withStyle(Div)`

  .hidden {

    display: none;

   }

`;

In the example above, for example, the element can be programmatically displayed and hidden.

Additional styles

There may be times when you need to add general styles to a page or target the children of DOM elements. In these cases you can make use of the renderStyle() method thus:

import withStyle from 'easy-with-style';   ///

const { renderStyle } = withStyle;

renderStyle(

  ...

);

This will create a separate style DOM element and place your style in there.

Building

Automation is thanks to npm scripts, have a look at the package.json file. The pertinent commands are:

npm run build-debug
npm run watch-debug

Contact

  • james.smith@djalbat.com
3.0.354

1 day ago

3.0.353

1 day ago

3.0.352

2 days ago

3.0.350

2 days ago

3.0.351

2 days ago

3.0.349

12 days ago

3.0.345

16 days ago

3.0.347

16 days ago

3.0.346

16 days ago

3.0.348

16 days ago

3.0.343

27 days ago

3.0.342

27 days ago

3.0.341

28 days ago

3.0.340

28 days ago

3.0.339

1 month ago

3.0.338

1 month ago

3.0.329

1 month ago

3.0.328

1 month ago

3.0.332

1 month ago

3.0.331

1 month ago

3.0.334

1 month ago

3.0.333

1 month ago

3.0.336

1 month ago

3.0.335

1 month ago

3.0.330

1 month ago

3.0.325

2 months ago

3.0.327

2 months ago

3.0.326

2 months ago

3.0.321

2 months ago

3.0.320

2 months ago

3.0.323

2 months ago

3.0.322

2 months ago

3.0.324

2 months ago

3.0.318

2 months ago

3.0.319

2 months ago

3.0.317

2 months ago

3.0.316

2 months ago

3.0.315

2 months ago

3.0.314

2 months ago

3.0.313

2 months ago

3.0.312

2 months ago

3.0.311

2 months ago

3.0.307

3 months ago

3.0.306

3 months ago

3.0.309

3 months ago

3.0.308

3 months ago

3.0.310

3 months ago

3.0.305

3 months ago

3.0.299

3 months ago

3.0.298

3 months ago

3.0.292

3 months ago

3.0.295

3 months ago

3.0.294

3 months ago

3.0.297

3 months ago

3.0.296

3 months ago

3.0.301

3 months ago

3.0.300

3 months ago

3.0.303

3 months ago

3.0.302

3 months ago

3.0.304

3 months ago

3.0.291

3 months ago

3.0.290

3 months ago

3.0.288

3 months ago

3.0.287

3 months ago

3.0.289

3 months ago

3.0.286

3 months ago

3.0.284

3 months ago

3.0.283

3 months ago

3.0.282

3 months ago

3.0.281

3 months ago

3.0.280

4 months ago

3.0.278

4 months ago

3.0.276

4 months ago

3.0.275

4 months ago

3.0.274

4 months ago

3.0.273

4 months ago

3.0.271

4 months ago

3.0.266

4 months ago

3.0.265

4 months ago

3.0.268

4 months ago

3.0.269

4 months ago

3.0.260

4 months ago

3.0.262

4 months ago

3.0.264

4 months ago

3.0.255

4 months ago

3.0.257

4 months ago

3.0.258

4 months ago

3.0.253

5 months ago

3.0.252

5 months ago

3.0.251

6 months ago

3.0.248

7 months ago

3.0.247

7 months ago

3.0.249

7 months ago

3.0.244

8 months ago

3.0.243

8 months ago

3.0.245

8 months ago

3.0.240

8 months ago

3.0.242

8 months ago

3.0.241

8 months ago

3.0.235

9 months ago

3.0.237

9 months ago

3.0.236

9 months ago

3.0.239

8 months ago

3.0.233

9 months ago

3.0.232

9 months ago

3.0.230

9 months ago

3.0.222

9 months ago

3.0.224

9 months ago

3.0.223

9 months ago

3.0.226

9 months ago

3.0.228

9 months ago

3.0.218

12 months ago

3.0.221

11 months ago

3.0.220

12 months ago

3.0.216

12 months ago

3.0.199

1 year ago

3.0.208

1 year ago

3.0.207

1 year ago

3.0.211

1 year ago

3.0.210

1 year ago

3.0.213

1 year ago

3.0.215

1 year ago

3.0.214

1 year ago

3.0.202

1 year ago

3.0.201

1 year ago

3.0.204

1 year ago

3.0.203

1 year ago

3.0.205

1 year ago

3.0.194

1 year ago

3.0.193

1 year ago

3.0.195

1 year ago

3.0.198

1 year ago

3.0.197

1 year ago

3.0.190

1 year ago

3.0.192

1 year ago

3.0.191

1 year ago

3.0.189

1 year ago

3.0.188

1 year ago

3.0.185

1 year ago

3.0.184

2 years ago

3.0.187

1 year ago

3.0.186

1 year ago

3.0.181

2 years ago

3.0.183

2 years ago

3.0.182

2 years ago

3.0.178

2 years ago

3.0.179

2 years ago

3.0.176

2 years ago

3.0.175

2 years ago

3.0.170

2 years ago

3.0.172

2 years ago

3.0.173

2 years ago

3.0.166

2 years ago

3.0.168

2 years ago

3.0.160

2 years ago

3.0.162

2 years ago

3.0.164

2 years ago

3.0.156

2 years ago

3.0.158

2 years ago

3.0.157

2 years ago

3.0.151

2 years ago

3.0.154

2 years ago

3.0.153

2 years ago

3.0.145

2 years ago

3.0.147

2 years ago

3.0.149

2 years ago

3.0.141

2 years ago

3.0.143

2 years ago

3.0.142

2 years ago

3.0.139

2 years ago

3.0.134

2 years ago

3.0.136

2 years ago

3.0.135

2 years ago

3.0.137

2 years ago

3.0.130

2 years ago

3.0.132

2 years ago

3.0.123

2 years ago

3.0.125

2 years ago

3.0.124

2 years ago

3.0.127

2 years ago

3.0.126

2 years ago

3.0.128

2 years ago

3.0.122

2 years ago

3.0.120

2 years ago

3.0.116

2 years ago

3.0.115

2 years ago

3.0.118

2 years ago

3.0.89

2 years ago

3.0.87

2 years ago

3.0.88

2 years ago

3.0.81

2 years ago

3.0.82

2 years ago

3.0.80

2 years ago

3.0.85

2 years ago

3.0.86

2 years ago

3.0.83

2 years ago

3.0.84

2 years ago

3.0.98

2 years ago

3.0.99

2 years ago

3.0.109

2 years ago

3.0.92

2 years ago

3.0.108

2 years ago

3.0.93

2 years ago

3.0.90

2 years ago

3.0.91

2 years ago

3.0.96

2 years ago

3.0.97

2 years ago

3.0.94

2 years ago

3.0.95

2 years ago

3.0.112

2 years ago

3.0.111

2 years ago

3.0.114

2 years ago

3.0.113

2 years ago

3.0.110

2 years ago

3.0.101

2 years ago

3.0.100

2 years ago

3.0.103

2 years ago

3.0.102

2 years ago

3.0.105

2 years ago

3.0.104

2 years ago

3.0.107

2 years ago

3.0.106

2 years ago

3.0.78

2 years ago

3.0.77

2 years ago

3.0.67

3 years ago

3.0.68

3 years ago

3.0.65

3 years ago

3.0.66

3 years ago

3.0.69

3 years ago

3.0.64

3 years ago

3.0.76

2 years ago

3.0.70

3 years ago

3.0.71

2 years ago

3.0.74

2 years ago

3.0.72

2 years ago

3.0.73

2 years ago

3.0.60

3 years ago

3.0.63

3 years ago

3.0.61

3 years ago

3.0.58

3 years ago

3.0.59

3 years ago

3.0.56

3 years ago

3.0.57

3 years ago

3.0.54

3 years ago

3.0.55

3 years ago

3.0.53

3 years ago

3.0.52

3 years ago

3.0.51

3 years ago

3.0.45

3 years ago

3.0.46

3 years ago

3.0.49

3 years ago

3.0.47

3 years ago

3.0.48

3 years ago

3.0.44

3 years ago

3.0.40

3 years ago

3.0.39

3 years ago

3.0.38

3 years ago

3.0.36

3 years ago

3.0.37

3 years ago

3.0.35

3 years ago

3.0.34

3 years ago

3.0.32

3 years ago

3.0.33

3 years ago

3.0.31

3 years ago

3.0.30

3 years ago

3.0.29

3 years ago

3.0.28

3 years ago

3.0.27

3 years ago

3.0.26

3 years ago

3.0.25

3 years ago

3.0.24

3 years ago

3.0.22

3 years ago

3.0.21

3 years ago

3.0.20

3 years ago

3.0.19

4 years ago

3.0.16

4 years ago

3.0.17

4 years ago

3.0.18

4 years ago

3.0.14

4 years ago

3.0.15

4 years ago

3.0.13

4 years ago

3.0.12

4 years ago

3.0.11

4 years ago

3.0.10

4 years ago

3.0.9

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

2.0.28

4 years ago

3.0.0

4 years ago

2.0.27

4 years ago

2.0.26

4 years ago

2.0.25

4 years ago

2.0.24

4 years ago

2.0.23

4 years ago

2.0.22

4 years ago

2.0.19

4 years ago

2.0.20

4 years ago

2.0.21

4 years ago

2.0.17

4 years ago

2.0.18

4 years ago

2.0.15

4 years ago

2.0.16

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.7

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.6

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago