0.0.48 • Published 3 years ago

junte-encapsuler v0.0.48

Weekly downloads
143
License
-
Repository
-
Last release
3 years ago

Junte Encapsuler

Version

npm v0.0.26

##Installation

  • Install Junte Encapsuler with npm install junte-encapsuler --save

  • Once installed you need add some scripts to package.json for encapsulate your components

"encapsuler": "mkdir -p src/scripts && cp ./node_modules/junte-encapsuler/encapsulation.ts./src/scripts",
"encapsulation": "gulp build --gulpfile ./src/scripts/encapsulation.ts",
"encapsulation:watch": "gulp build --gulpfile ./src/scripts/encapsulation.ts --watch"

  • Use npm run encapsuler for copy gulpfile from node_modules to your_project/scripts/encapsulation.ts

  • Use npm run encapsulation (:watch) for encapsulate your components templates and styles.

##Encapsulation

###host and child-of attributes:

In order for our encapsulation to work, you need to adds the host='selector' attribute using @HostBinding, this will provide an encapsulation of component styles.

To encapsulate the styles of elements inside a component, our encapsulator automatically adds the child-of='selector' attribute to them.

###child-host attribute:

To override the styles of the encapsulated component, use the child-host=#{'$child-selector'} attribute in the parent's scss file

##Examples

Bind host attribute to test.component.ts file

@HostBinding('attr.host') host = 'app-test-host';

Add this host to any loaded scss style (variables.scss for example)

$app-test-host: 'app-test-host';

Change templateUrl and remove styleUrls to test.encapsulated

templateUrl: './test.encapsulated.html'

Import encapsulated style to global loaded styles

@import "components/test/test.encapsulated";

###host and child-of example:

test.component.html before

<h1>Test component</h1>
<form>
    <input type="text">
    <button>Test button</button>
</form>

test.encapsulated.html after:

<h1 child-of="app-test-host">Test component</h1>
<form child-of="app-test-host">
    <input child-of="app-test-host" type="text"/>
    <button child-of="app-test-host">Test button</button>
</form>

test.component.scss before:

@import "variables";

:host {
    display: block;
    padding: 16px;
    border: 1px solid $primary-color;
}

h1 {
    color: $primary-color;
}

form {
    background-color: $secondary-background;
    padding: 8px;

    input {
    margin-right: 16px;
    border-radius: $corner-normal;
    padding: 8px 16px;
  }

  button {
    background-color: $primary-color;
    color: white;
    border-radius: $corner-normal;
    padding: 8px 16px;
  }
}

test.encapsulated.scss after:

@import "variables";

app-test[host=#{$app-test-host}] {
    display: block;
    padding: 16px;
    border: 1px solid $primary-color;
}

h1[child-of=#{$app-test-host}] {
    color: $primary-color;
}

form[child-of=#{$app-test-host}] {
    background-color: $secondary-background;
    padding: 8px;

    input[child-of=#{$app-test-host}] {
    margin-right: 16px;
    border-radius: $corner-normal;
    padding: 8px 16px;
  }

  button[child-of=#{$app-test-host}] {
    background-color: $primary-color;
    color: white;
    border-radius: $corner-normal;
    padding: 8px 16px;
  }
}

###child-host example:

test-child.component.scss before:

@import "variables";

:host {
    display: block;
    background-color: #4F4F4F;
    padding: 16px;
    margin: 8px 0;
    color: white;
}

test-child.encapsulated.scss after:

@import "variables";

app-test-child[host=#{$app-test-child-host}] {
    display: block;
    background-color: #4F4F4F;
    padding: 16px;
    margin: 8px 0;
    color: white;
}

test-parent.component.scss before:

@import "variables";

:host {
    display: block;
    background-color: #E9E9E9;
    padding: 16px;
    margin: 8px 0;
}

app-test-child[child-host=#{$app-test-child-host}] {
    background-color: white;
    color: #4F4F4F;
}

test-parent.encapsulated.scss after:

@import "variables";

app-test-parent[host=#{$app-test-parent-host}] {
    display: block;
    background-color: #E9E9E9;
    padding: 16px;
    margin: 8px 0;
}

app-test-child[host=#{$app-test-child-host}][child-of=#{$app-test-parent-host}] {
    background-color: white;
    color: #4F4F4F;
}
0.0.47

3 years ago

0.0.48

3 years ago

0.0.46

3 years ago

0.0.45

4 years ago

0.0.43

4 years ago

0.0.44

4 years ago

0.0.42

4 years ago

0.0.41

4 years ago

0.0.40

4 years ago

0.0.39

4 years ago

0.0.38

4 years ago

0.0.37

4 years ago

0.0.36

4 years ago

0.0.33

4 years ago

0.0.34

4 years ago

0.0.35

4 years ago

0.0.32

4 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago