├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── ci ├── features.sh ├── run_build.sh ├── run_build_docs.sh ├── run_checks.sh └── run_tests.sh ├── examples ├── seed-testbed │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE_APACHE │ ├── LICENSE_MIT │ ├── README.md │ ├── bootstrap.js │ ├── package.json │ ├── src │ │ ├── app.rs │ │ ├── lib.rs │ │ └── utils.rs │ ├── static │ │ └── index.html │ ├── webpack.config.js │ └── yarn.lock ├── yew-integration │ ├── Cargo.toml │ ├── README.md │ ├── src │ │ └── main.rs │ └── static │ │ └── index.html └── yew-testbed │ ├── .gitignore │ ├── .travis.yml │ ├── Cargo.toml │ ├── LICENSE_APACHE │ ├── LICENSE_MIT │ ├── README.md │ ├── bootstrap.js │ ├── package.json │ ├── src │ ├── app.rs │ ├── app.scss │ ├── lib.rs │ └── utils.rs │ ├── static │ └── index.html │ ├── tests │ └── web.rs │ ├── webpack.config.js │ └── yarn.lock └── src ├── bindings ├── mod.rs ├── seed.rs └── yew.rs ├── lib.rs ├── parser.rs └── style ├── ast.rs └── mod.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Rust 2 | /target 3 | /examples/*/target 4 | Cargo.lock 5 | 6 | # VSCode 7 | *.code-workspace 8 | 9 | # MacOS 10 | ## General 11 | .DS_Store 12 | .AppleDouble 13 | .LSOverride 14 | 15 | ## Icon must end with two \r 16 | Icon 17 | 18 | ## Thumbnails 19 | ._* 20 | 21 | ## Files that might appear in the root of a volume 22 | .DocumentRevisions-V100 23 | .fseventsd 24 | .Spotlight-V100 25 | .TemporaryItems 26 | .Trashes 27 | .VolumeIcon.icns 28 | .com.apple.timemachine.donotpresent 29 | 30 | ## Directories potentially created on remote AFP share 31 | .AppleDB 32 | .AppleDesktop 33 | Network Trash Folder 34 | Temporary Items 35 | .apdisk 36 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | os: linux 3 | dist: bionic 4 | #addons: 5 | # firefox: latest 6 | 7 | cache: cargo 8 | before_cache: 9 | - ./ci/clear_cache.sh 10 | 11 | rust: 12 | - 1.42.0 # min supported (because of yew) 13 | - stable 14 | - beta 15 | 16 | jobs: 17 | allow_failures: 18 | - rust: beta 19 | fast_finish: true 20 | 21 | install: 22 | - rustup component add rustfmt 23 | - rustup component add clippy 24 | - rustup target add wasm32-unknown-unknown 25 | - cargo install cargo-update || true 26 | - cargo install-update-config --version =0.2.59 wasm-bindgen-cli 27 | - cargo install-update --allow-no-update wasm-bindgen-cli 28 | - '[ -f /home/travis/.cargo/bin/wasm-pack ] && echo "wasm-pack already installed" || curl --retry 5 https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh' 29 | # - curl --retry 5 -LO https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz 30 | # - tar -xzf geckodriver-v0.26.0-linux64.tar.gz 31 | 32 | script: 33 | - ./ci/run_checks.sh 34 | - ./ci/run_tests.sh 35 | # - ./ci/run_build_docs.sh 36 | - ./ci/run_build.sh 37 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "css-in-rust" 3 | version = "0.5.0" 4 | license = "MIT" 5 | repository = "https://github.com/lukidoescode/css-in-rust" 6 | authors = [ 7 | "Lukas Wagner ", 8 | ] 9 | edition = "2018" 10 | description = "CSSinRust is a package for use with WASM applications providing a component level CSS styling experience." 11 | keywords = [ 12 | "CSS", 13 | "web", 14 | "CSSinRust", 15 | "yew" 16 | ] 17 | categories = ["wasm", "web-programming"] 18 | readme = "README.md" 19 | homepage = "https://crates.io/crates/css-in-rust" 20 | 21 | 22 | [lib] 23 | crate-type = ["cdylib", "rlib"] 24 | 25 | [dependencies] 26 | nom = "^5.1.1" 27 | lazy_static = "^1.4.0" 28 | yew = {version = "^0.17.2", features=["web_sys"], optional = true} 29 | seed = {version = "^0.6.0", optional = true} 30 | 31 | [target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] 32 | version = "^0.3" 33 | features = [ 34 | "Window", 35 | "Document", 36 | "Element", 37 | "HtmlElement", 38 | "HtmlHeadElement", 39 | "HtmlStyleElement", 40 | ] 41 | 42 | [target.'cfg(not(target_arch = "wasm32"))'.dependencies] 43 | rand = { version = "^0.7.0", features = ["small_rng"]} 44 | 45 | # Changes here must be reflected in `build.rs` 46 | [target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dependencies] 47 | wasm-bindgen = "^0.2.59" 48 | 49 | # Changes here must be reflected in `build.rs` 50 | [target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dev-dependencies] 51 | wasm-bindgen-test = "^0.3.9" 52 | 53 | [features] 54 | yew_integration = ["yew"] 55 | seed_integration = ["seed"] 56 | 57 | [package.metadata.docs.rs] 58 | features = ["yew_integration", "seed_integration"] 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Lukas Wagner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.com/lukidoescode/css-in-rust.svg?branch=master)](https://travis-ci.com/lukidoescode/css-in-rust) 2 | 3 | # CSSinRust 4 | 5 | CSSinRust delivers a new way of implementing CSS styling in web-sys applications. 6 | It's aim is to make writing web frontends in Rust attractive by providing a way to style on a component level. The library is implemented so that it could in theory work with any framework. However, right now there is only an implementation for yew. Pull requests are very welcome, whether for improving code quality, integration solutions or functionality. 7 | 8 | Please be aware that this project is still under heavy development and that future changes might break your code. I'm still not sure about the overall design but I needed something like this and I'm sure some other people do as well. 9 | 10 | If you'd like to be kept up to date or you'd like to support my work please visit me on those platforms: 11 | 12 | - [Twitter](https://twitter.com/lukidoescode) 13 | - [Patreon](https://www.patreon.com/lukaswagner) 14 | 15 | # Syntax 16 | 17 | Currently there is only support for a very basic set of syntax. Even though the syntax is very similar to CSS there are a few particularities in CSSinRust which are inspired by SASS and styled-components in JS. 18 | 19 | Here is how a basic style would get defined. 20 | 21 | ```rust 22 | let style = match css_in_rust::Style::create( 23 | "Component", // The class prefix 24 | // The actual css 25 | r#" 26 | background-color: red; 27 | 28 | .nested { 29 | background-color: blue; 30 | width: 100px 31 | }"#, 32 | ) { 33 | Ok(style) => style, 34 | Err(error) => { 35 | panic!("An error occured while creating the style: {}", error); 36 | } 37 | }; 38 | ``` 39 | 40 | So everything that is not in a conditioned block will be applied to the Component the class of this style is applied to. How that happens depends on the framework to use. Below there are examples for the supported frameworks. 41 | 42 | The Style you put in will get parsed and converted to actual CSS and automatically appended to the head of your HTML document. 43 | 44 | You may also use the `&` identifier in order to use CSS selectors or pseudo classes on the styled element: 45 | 46 | ```css 47 | &:hover { 48 | background-color: #d0d0d9; 49 | } 50 | ``` 51 | 52 | You can also use other CSS rules, e.g. keyframes: 53 | 54 | ```css 55 | @keyframes mymove { 56 | from { 57 | top: 0px; 58 | } 59 | to { 60 | top: 200px; 61 | } 62 | } 63 | ``` 64 | 65 | Please be aware that right now, CSSinRust will not parse the name of the animation in order to make it unique. If you need that feature please upvote the issue or open a new one if there is none already. 66 | 67 | There is also media query support now! 68 | 69 | ```css 70 | @media only screen and (max-width: 600px) { 71 | background-color: #303040; 72 | 73 | .nested { 74 | background-color: lightblue; 75 | } 76 | 77 | &:hover { 78 | background-color: #606072; 79 | } 80 | } 81 | ``` 82 | 83 | # Integrations 84 | 85 | ## Seed 86 | 87 | In order to enable all yew integration use the feature `seed_integration` for CSSinRust in your `Cargo.toml`. Then create a style and use it with yew like this: 88 | 89 | ```rust 90 | pub(crate) struct Model { 91 | pub style: css_in_rust::Style, 92 | } 93 | 94 | impl Default for Model { 95 | fn default() -> Self { 96 | let style = match css_in_rust::Style::create( 97 | String::from("Component"), 98 | String::from( 99 | r#" 100 | background-color: #303040; 101 | color: #DDDDDD; 102 | padding: 5px; 103 | &:hover { 104 | background-color: #606072; 105 | } 106 | "#, 107 | ), 108 | ) { 109 | Ok(style) => style, 110 | Err(error) => { 111 | panic!("An error occured while creating the style: {}", error); 112 | } 113 | }; 114 | Self { 115 | style: style, 116 | } 117 | } 118 | } 119 | 120 | #[derive(Clone)] 121 | pub(crate) enum Msg { 122 | } 123 | 124 | pub(crate) fn update(msg: Msg, model: &mut Model, _: &mut impl Orders) { 125 | } 126 | 127 | pub(crate) fn view(model: &Model) -> impl View { 128 | div![ 129 | model.style.clone(), 130 | "Hello, World" 131 | ] 132 | } 133 | ``` 134 | 135 | ## Yew 136 | 137 | In order to enable all yew integration use the feature `yew_integration` for CSSinRust in your `Cargo.toml`. Then create a style and use it with yew like this: 138 | 139 | ```rust 140 | impl Component for HelloComponent { 141 | type Message = (); 142 | type Properties = (); 143 | 144 | fn create(_: Self::Properties, _: ComponentLink) -> Self { 145 | let style = match css_in_rust::Style::create( 146 | "Component", 147 | "background-color: #505050;", 148 | ) { 149 | Ok(style) => style, 150 | Err(error) => { 151 | panic!("An error occured while creating the style: {}", error); 152 | } 153 | }; 154 | HelloComponent { 155 | style, 156 | } 157 | } 158 | 159 | fn update(&mut self, _: Self::Message) -> ShouldRender { 160 | true 161 | } 162 | 163 | fn view(&self) -> Html { 164 | html! {
{"Hello World!"}
} 165 | } 166 | } 167 | ``` 168 | 169 | ### CSSinRust Versions and Corresponding Yew Versions 170 | 171 | | Yew Version | CSSinRust Version | 172 | | ----------- | ----------------- | 173 | | 0.14.x | 0.2.2 | 174 | | 0.15.x | 0.3.x | 175 | | 0.16.x | 0.4.x | 176 | | 0.17.x | 0.5.x | 177 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | // Props to the Yew team https://github.com/yewstack/yew/blob/master/build.rs 2 | 3 | use std::env; 4 | 5 | pub fn main() { 6 | let using_cargo_web = env::var("COMPILING_UNDER_CARGO_WEB").is_ok(); 7 | if using_cargo_web { 8 | panic!("cargo-web is not compatible with web-sys"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ci/features.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export NON_CONFLICTING_FEATURES="yew_integration,seed_integration" 4 | -------------------------------------------------------------------------------- /ci/run_build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 4 | . "$DIR/features.sh" 5 | 6 | wasm-pack build -- --features "$NON_CONFLICTING_FEATURES" 7 | -------------------------------------------------------------------------------- /ci/run_build_docs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 4 | . "$DIR/features.sh" 5 | 6 | cargo doc --features "$NON_CONFLICTING_FEATURES" 7 | -------------------------------------------------------------------------------- /ci/run_checks.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo "$(rustup default)" | grep -q "stable" 4 | if [ "$?" != "0" ]; then 5 | # only run checks on stable 6 | exit 0 7 | fi 8 | 9 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 10 | . "$DIR/features.sh" 11 | 12 | set -euxo pipefail 13 | cargo fmt --all -- --check 14 | cargo clippy --features "$NON_CONFLICTING_FEATURES" -- --deny=warnings 15 | -------------------------------------------------------------------------------- /ci/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This is blocked by https://github.com/rustwasm/wasm-pack/issues/698 4 | #wasm-pack test --node -- --features std_web 5 | cargo test 6 | -------------------------------------------------------------------------------- /examples/seed-testbed/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | bin/ 5 | pkg/ 6 | dist/ 7 | wasm-pack.log 8 | node_modules 9 | 10 | -------------------------------------------------------------------------------- /examples/seed-testbed/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "seed-testbed" 3 | version = "0.0.1" 4 | authors = ["Lukas Wagner "] 5 | edition = "2018" 6 | publish = false 7 | 8 | [lib] 9 | crate-type = ["cdylib", "rlib"] 10 | 11 | [features] 12 | default = ["console_error_panic_hook"] 13 | 14 | [dependencies] 15 | log = "0.4.8" 16 | serde = "1" 17 | serde_derive = "1" 18 | wasm-bindgen = "0.2.60" 19 | web_logger = "0.2" 20 | seed = "^0.6.0" 21 | 22 | css-in-rust = { path = "../..", features = ["seed_integration"]} 23 | 24 | # The `console_error_panic_hook` crate provides better debugging of panics by 25 | # logging them with `console.error`. This is great for development, but requires 26 | # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for 27 | # code size when deploying. 28 | console_error_panic_hook = { version = "0.1.6", optional = true } 29 | 30 | # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size 31 | # compared to the default allocator's ~10K. It is slower than the default 32 | # allocator, however. 33 | wee_alloc = { version = "0.4.5", optional = true } 34 | 35 | [dev-dependencies] 36 | wasm-bindgen-test = "0.3.10" 37 | 38 | [profile.release] # Attempts to minimize file size 39 | lto = true 40 | opt-level = 'z' -------------------------------------------------------------------------------- /examples/seed-testbed/LICENSE_APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /examples/seed-testbed/LICENSE_MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 {{authors}} 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /examples/seed-testbed/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This crate is for testing the seed integration of CSSinRust. 4 | 5 | ## 🚴 Usage 6 | 7 | ### 🛠️ Build with `yarn run build` 8 | 9 | ``` 10 | yarn run build 11 | ``` 12 | 13 | ### 🔬 Serve locally with `yarn run start:dev` 14 | 15 | ``` 16 | yarn run start:dev 17 | ``` 18 | 19 | 20 | ## 🔋 Batteries Included 21 | 22 | * [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating 23 | between WebAssembly and JavaScript. 24 | * [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) 25 | for logging panic messages to the developer console. 26 | * [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized 27 | for small code size. 28 | -------------------------------------------------------------------------------- /examples/seed-testbed/bootstrap.js: -------------------------------------------------------------------------------- 1 | import("./pkg").then(module => { 2 | module.run_app(); 3 | }); 4 | -------------------------------------------------------------------------------- /examples/seed-testbed/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "test": "echo \"Error: no test specified\" && exit 1", 5 | "dev": "webpack --mode development", 6 | "build": "webpack --mode production", 7 | "start:dev": "webpack-dev-server --mode development" 8 | }, 9 | "devDependencies": { 10 | "@wasm-tool/wasm-pack-plugin": "^1.0.0", 11 | "copy-webpack-plugin": "^5.0.4", 12 | "webpack": "^4.29.3", 13 | "webpack-cli": "^3.1.2", 14 | "webpack-dev-server": "^3.7.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /examples/seed-testbed/src/app.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | extern crate css_in_rust; 4 | 5 | use css_in_rust::Style; 6 | use seed::{prelude::*, *}; 7 | 8 | pub(crate) struct Model { 9 | pub val: i32, 10 | pub style: Style, 11 | } 12 | 13 | impl Default for Model { 14 | fn default() -> Self { 15 | let style: Style = Style::create( 16 | String::from("App"), 17 | String::from( 18 | r#" 19 | background-color: #303040; 20 | color: #DDDDDD; 21 | padding: 5px; 22 | &:hover { 23 | background-color: #606072; 24 | } 25 | "#, 26 | ), 27 | ) 28 | .unwrap(); 29 | Self { 30 | val: 0, 31 | style: style, 32 | } 33 | } 34 | } 35 | 36 | #[derive(Clone)] 37 | pub(crate) enum Msg { 38 | Increment, 39 | } 40 | 41 | pub(crate) fn update(msg: Msg, model: &mut Model, _: &mut impl Orders) { 42 | match msg { 43 | Msg::Increment => model.val += 1, 44 | } 45 | } 46 | 47 | pub(crate) fn view(model: &Model) -> impl View { 48 | button![ 49 | model.style.clone(), 50 | simple_ev(Ev::Click, Msg::Increment), 51 | format!("Hello, World × {}", model.val) 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /examples/seed-testbed/src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | #![recursion_limit = "512"] 4 | 5 | macro_rules! println { 6 | ($($tt:tt)*) => {{ 7 | let msg = format!($($tt)*); 8 | js! { console.log(@{ msg }) } 9 | }} 10 | } 11 | 12 | mod app; 13 | mod utils; 14 | 15 | use seed::prelude::*; 16 | use wasm_bindgen::prelude::*; 17 | 18 | // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global 19 | // allocator. 20 | #[cfg(feature = "wee_alloc")] 21 | #[global_allocator] 22 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 23 | 24 | // This is the entry point for the web app 25 | #[wasm_bindgen] 26 | pub fn run_app() -> Result<(), JsValue> { 27 | utils::set_panic_hook(); 28 | web_logger::init(); 29 | seed::App::builder(app::update, app::view).build_and_start(); 30 | Ok(()) 31 | } 32 | -------------------------------------------------------------------------------- /examples/seed-testbed/src/utils.rs: -------------------------------------------------------------------------------- 1 | pub fn set_panic_hook() { 2 | // When the `console_error_panic_hook` feature is enabled, we can call the 3 | // `set_panic_hook` function at least once during initialization, and then 4 | // we will get better error messages if our code ever panics. 5 | // 6 | // For more details see 7 | // https://github.com/rustwasm/console_error_panic_hook#readme 8 | #[cfg(feature = "console_error_panic_hook")] 9 | console_error_panic_hook::set_once(); 10 | } 11 | -------------------------------------------------------------------------------- /examples/seed-testbed/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CSSinRust Seed Testbed 6 | 7 | 8 |
9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/seed-testbed/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin'); 3 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 4 | 5 | const distPath = path.resolve(__dirname, 'dist'); 6 | module.exports = (env, argv) => { 7 | return { 8 | devServer: { 9 | contentBase: distPath, 10 | compress: argv.mode === 'production', 11 | port: 8000, 12 | }, 13 | entry: './bootstrap.js', 14 | output: { 15 | path: distPath, 16 | filename: 'seed-testbed.js', 17 | webassemblyModuleFilename: 'seed-testbed.wasm' 18 | }, 19 | plugins: [ 20 | argv.mode !== 'production' ? function(compiler) { 21 | // This plugin enables recompilation whenever stuff in rust has changed 22 | compiler.hooks.afterCompile.tap('CSSinRust', (compilation) => { 23 | compilation.contextDependencies.add(path.resolve(__dirname, '../../src')); 24 | return true; 25 | }); 26 | } : undefined, 27 | new CopyWebpackPlugin([ 28 | { from: './static', to: distPath } 29 | ]), 30 | new WasmPackPlugin({ 31 | crateDirectory: '.', 32 | extraArgs: '--no-typescript', 33 | }), 34 | ], 35 | watch: argv.mode !== 'production', 36 | }; 37 | }; 38 | -------------------------------------------------------------------------------- /examples/yew-integration/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "yew_integration" 3 | version = "0.0.1" 4 | authors = ["Lukas Wagner "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | log = "0.4" 9 | web_logger = "0.2" 10 | yew = {version = "0.16.0", features=["web_sys"]} 11 | css-in-rust = { path = "../..", features = ["yew_integration"]} 12 | -------------------------------------------------------------------------------- /examples/yew-integration/README.md: -------------------------------------------------------------------------------- 1 | # Quickstart 2 | 3 | `rustup update` 4 | 5 | `rustup target add wasm32-unknown-unknown` 6 | 7 | `cargo install cargo-make` 8 | 9 | Run `cargo make build` in a terminal to build the app, and `cargo make serve` to start a dev server 10 | on `127.0.0.1:8000`. 11 | 12 | If you'd like the compiler automatically check for changes, recompiling as 13 | needed, run `cargo make watch` instead of `cargo make build`. -------------------------------------------------------------------------------- /examples/yew-integration/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate css_in_rust; 2 | extern crate log; 3 | extern crate yew; 4 | 5 | use css_in_rust::style::Style; 6 | use log::trace; 7 | use yew::App; 8 | use yew::{html, Component, ComponentLink, Html, ShouldRender}; 9 | 10 | pub struct CustomComponent { 11 | style: Style, 12 | } 13 | 14 | impl Component for CustomComponent { 15 | type Message = (); 16 | type Properties = (); 17 | 18 | fn create(_: Self::Properties, _: ComponentLink) -> Self { 19 | let style = Style::create( 20 | "CustomComponent", 21 | r#" 22 | background-color: red; 23 | .on-da-inside { 24 | background-color: blue; 25 | width: 100px 26 | } 27 | "#, 28 | ) 29 | .unwrap(); 30 | CustomComponent { style } 31 | } 32 | 33 | fn update(&mut self, _: Self::Message) -> ShouldRender { 34 | true 35 | } 36 | 37 | fn change(&mut self, _: ::Properties) -> bool { 38 | false 39 | } 40 | 41 | fn view(&self) -> Html { 42 | html! {
43 | {"The quick brown fox jumps over the lazy dog"} 44 |
{"The quick brown fox jumps over the lazy dog"}
45 |
} 46 | } 47 | } 48 | fn main() { 49 | web_logger::init(); 50 | yew::start_app::(); 51 | } 52 | -------------------------------------------------------------------------------- /examples/yew-integration/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Yew Integration Example 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /examples/yew-testbed/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | bin/ 5 | pkg/ 6 | dist/ 7 | wasm-pack.log 8 | node_modules 9 | 10 | -------------------------------------------------------------------------------- /examples/yew-testbed/.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | sudo: false 3 | 4 | cache: cargo 5 | 6 | matrix: 7 | include: 8 | 9 | # Builds with wasm-pack. 10 | - rust: beta 11 | env: RUST_BACKTRACE=1 12 | addons: 13 | firefox: latest 14 | chrome: stable 15 | before_script: 16 | - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) 17 | - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) 18 | - cargo install-update -a 19 | - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f 20 | script: 21 | - cargo generate --git . --name testing 22 | # Having a broken Cargo.toml (in that it has curlies in fields) anywhere 23 | # in any of our parent dirs is problematic. 24 | - mv Cargo.toml Cargo.toml.tmpl 25 | - cd testing 26 | - wasm-pack build 27 | - wasm-pack test --chrome --firefox --headless 28 | 29 | # Builds on nightly. 30 | - rust: nightly 31 | env: RUST_BACKTRACE=1 32 | before_script: 33 | - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) 34 | - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) 35 | - cargo install-update -a 36 | - rustup target add wasm32-unknown-unknown 37 | script: 38 | - cargo generate --git . --name testing 39 | - mv Cargo.toml Cargo.toml.tmpl 40 | - cd testing 41 | - cargo check 42 | - cargo check --target wasm32-unknown-unknown 43 | - cargo check --no-default-features 44 | - cargo check --target wasm32-unknown-unknown --no-default-features 45 | - cargo check --no-default-features --features console_error_panic_hook 46 | - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook 47 | - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" 48 | - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" 49 | 50 | # Builds on beta. 51 | - rust: beta 52 | env: RUST_BACKTRACE=1 53 | before_script: 54 | - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) 55 | - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) 56 | - cargo install-update -a 57 | - rustup target add wasm32-unknown-unknown 58 | script: 59 | - cargo generate --git . --name testing 60 | - mv Cargo.toml Cargo.toml.tmpl 61 | - cd testing 62 | - cargo check 63 | - cargo check --target wasm32-unknown-unknown 64 | - cargo check --no-default-features 65 | - cargo check --target wasm32-unknown-unknown --no-default-features 66 | - cargo check --no-default-features --features console_error_panic_hook 67 | - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook 68 | # Note: no enabling the `wee_alloc` feature here because it requires 69 | # nightly for now. 70 | -------------------------------------------------------------------------------- /examples/yew-testbed/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "yew-testbed" 3 | version = "0.0.2" 4 | authors = ["Lukas Wagner "] 5 | edition = "2018" 6 | publish = false 7 | 8 | [lib] 9 | crate-type = ["cdylib", "rlib"] 10 | 11 | [features] 12 | default = ["console_error_panic_hook"] 13 | 14 | [dependencies] 15 | log = "0.4.8" 16 | serde = "1" 17 | serde_derive = "1" 18 | wasm-bindgen = "0.2.59" 19 | web_logger = "0.2" 20 | yew = { version = "^0.17.2", features = ["web_sys"] } 21 | 22 | css-in-rust = { path = "../..", features = ["yew_integration"]} 23 | 24 | # The `console_error_panic_hook` crate provides better debugging of panics by 25 | # logging them with `console.error`. This is great for development, but requires 26 | # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for 27 | # code size when deploying. 28 | console_error_panic_hook = { version = "0.1.6", optional = true } 29 | 30 | # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size 31 | # compared to the default allocator's ~10K. It is slower than the default 32 | # allocator, however. 33 | wee_alloc = { version = "0.4.5", optional = true } 34 | 35 | [dev-dependencies] 36 | wasm-bindgen-test = "0.3.8" 37 | -------------------------------------------------------------------------------- /examples/yew-testbed/LICENSE_APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /examples/yew-testbed/LICENSE_MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 {{authors}} 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /examples/yew-testbed/README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | This crate is for testing the yew integration of CSSinRust. 4 | 5 | ## 🚴 Usage 6 | 7 | ### 🛠️ Build with `yarn run build` 8 | 9 | ``` 10 | yarn run build 11 | ``` 12 | 13 | ### 🔬 Serve locally with `yarn run start:dev` 14 | 15 | ``` 16 | yarn run start:dev 17 | ``` 18 | 19 | 20 | ## 🔋 Batteries Included 21 | 22 | * [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating 23 | between WebAssembly and JavaScript. 24 | * [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) 25 | for logging panic messages to the developer console. 26 | * [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized 27 | for small code size. 28 | -------------------------------------------------------------------------------- /examples/yew-testbed/bootstrap.js: -------------------------------------------------------------------------------- 1 | import("./pkg").then(module => { 2 | module.run_app(); 3 | }); 4 | -------------------------------------------------------------------------------- /examples/yew-testbed/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "test": "echo \"Error: no test specified\" && exit 1", 5 | "dev": "webpack --mode development", 6 | "build": "webpack --mode production", 7 | "start:dev": "webpack-dev-server --mode development" 8 | }, 9 | "devDependencies": { 10 | "@wasm-tool/wasm-pack-plugin": "^1.0.0", 11 | "copy-webpack-plugin": "^5.0.4", 12 | "webpack": "^4.29.3", 13 | "webpack-cli": "^3.1.2", 14 | "webpack-dev-server": "^3.7.2" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /examples/yew-testbed/src/app.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | extern crate css_in_rust; 4 | 5 | use css_in_rust::Style; 6 | use yew::{html, Component, ComponentLink, Html, ShouldRender}; 7 | 8 | pub struct App { 9 | style: Style, 10 | } 11 | 12 | impl Component for App { 13 | type Message = (); 14 | type Properties = (); 15 | 16 | fn create(_: Self::Properties, _: ComponentLink) -> Self { 17 | let style = match Style::create("App", include_str!("app.scss")) { 18 | Ok(style) => style, 19 | Err(error) => { 20 | panic!("An error occured while creating the style: {}", error); 21 | } 22 | }; 23 | App { style } 24 | } 25 | 26 | fn update(&mut self, _: Self::Message) -> ShouldRender { 27 | true 28 | } 29 | 30 | fn change(&mut self, _: ::Properties) -> bool { 31 | false 32 | } 33 | 34 | fn view(&self) -> Html { 35 | html! {
36 | {"The quick brown fox jumps over the lazy dog"} 37 |
{"The quick brown fox jumps over the lazy dog"}
38 |
} 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /examples/yew-testbed/src/app.scss: -------------------------------------------------------------------------------- 1 | /* Copyright © 2020 Lukas Wagner */ 2 | 3 | & { 4 | background-color: #303040; 5 | color: #dddddd; 6 | padding: 5px; 7 | } 8 | 9 | &:hover { 10 | background-color: #606072; 11 | } 12 | 13 | .on-da-inside { 14 | background-color: blue; 15 | width: 100px; 16 | color: #ddd; 17 | padding: 5px; 18 | animation: move 2s infinite; 19 | animation-timing-function: linear; 20 | animation-direction: alternate; 21 | } 22 | 23 | @keyframes move { 24 | from { 25 | width: 100px; 26 | } 27 | to { 28 | width: 200px; 29 | } 30 | } 31 | 32 | @media only screen and (min-width: 626px) { 33 | width: 600px; 34 | margin: auto; 35 | 36 | @keyframes move { 37 | from { 38 | width: 100px; 39 | } 40 | to { 41 | width: 590px; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /examples/yew-testbed/src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | #![recursion_limit = "512"] 4 | 5 | macro_rules! println { 6 | ($($tt:tt)*) => {{ 7 | let msg = format!($($tt)*); 8 | js! { console.log(@{ msg }) } 9 | }} 10 | } 11 | 12 | mod app; 13 | mod utils; 14 | 15 | use wasm_bindgen::prelude::*; 16 | 17 | // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global 18 | // allocator. 19 | #[cfg(feature = "wee_alloc")] 20 | #[global_allocator] 21 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 22 | 23 | // This is the entry point for the web app 24 | #[wasm_bindgen] 25 | pub fn run_app() -> Result<(), JsValue> { 26 | utils::set_panic_hook(); 27 | web_logger::init(); 28 | yew::start_app::(); 29 | Ok(()) 30 | } 31 | -------------------------------------------------------------------------------- /examples/yew-testbed/src/utils.rs: -------------------------------------------------------------------------------- 1 | pub fn set_panic_hook() { 2 | // When the `console_error_panic_hook` feature is enabled, we can call the 3 | // `set_panic_hook` function at least once during initialization, and then 4 | // we will get better error messages if our code ever panics. 5 | // 6 | // For more details see 7 | // https://github.com/rustwasm/console_error_panic_hook#readme 8 | #[cfg(feature = "console_error_panic_hook")] 9 | console_error_panic_hook::set_once(); 10 | } 11 | -------------------------------------------------------------------------------- /examples/yew-testbed/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CSSinRust Yew Testbed 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /examples/yew-testbed/tests/web.rs: -------------------------------------------------------------------------------- 1 | //! Test suite for the Web and headless browsers. 2 | 3 | #![cfg(target_arch = "wasm32")] 4 | 5 | extern crate wasm_bindgen_test; 6 | use wasm_bindgen_test::*; 7 | 8 | wasm_bindgen_test_configure!(run_in_browser); 9 | 10 | #[wasm_bindgen_test] 11 | fn pass() { 12 | assert_eq!(1 + 1, 2); 13 | } 14 | -------------------------------------------------------------------------------- /examples/yew-testbed/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); 3 | const CopyWebpackPlugin = require("copy-webpack-plugin"); 4 | 5 | const distPath = path.resolve(__dirname, "dist"); 6 | module.exports = (env, argv) => { 7 | return { 8 | devServer: { 9 | contentBase: distPath, 10 | compress: argv.mode === "production", 11 | port: 5050, 12 | }, 13 | entry: "./bootstrap.js", 14 | output: { 15 | path: distPath, 16 | filename: "yew-testbed.js", 17 | webassemblyModuleFilename: "yew-testbed.wasm", 18 | }, 19 | plugins: [ 20 | argv.mode !== "production" 21 | ? function (compiler) { 22 | // This plugin enables recompilation whenever stuff in rust has changed 23 | compiler.hooks.afterCompile.tap("CSSinRust", (compilation) => { 24 | compilation.contextDependencies.add( 25 | path.resolve(__dirname, "../../src") 26 | ); 27 | return true; 28 | }); 29 | } 30 | : undefined, 31 | new CopyWebpackPlugin([{ from: "./static", to: distPath }]), 32 | new WasmPackPlugin({ 33 | crateDirectory: ".", 34 | extraArgs: "--no-typescript", 35 | }), 36 | ], 37 | watch: argv.mode !== "production", 38 | }; 39 | }; 40 | -------------------------------------------------------------------------------- /src/bindings/mod.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | #[cfg(feature = "yew")] 4 | pub mod yew; 5 | 6 | #[cfg(feature = "seed")] 7 | pub mod seed; 8 | -------------------------------------------------------------------------------- /src/bindings/seed.rs: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Lukas Wagner 2 | 3 | //! Seed integration module. 4 | //! The user doesn't need to do anything but to add a style into a 5 | //! seed component. 6 | 7 | #[cfg(target_arch = "wasm32")] 8 | extern crate seed; 9 | 10 | #[cfg(target_arch = "wasm32")] 11 | use super::super::style::Style; 12 | 13 | #[cfg(target_arch = "wasm32")] 14 | use seed::virtual_dom::{At, AtValue, Attrs, El, UpdateEl}; 15 | 16 | #[cfg(target_arch = "wasm32")] 17 | impl UpdateEl> for Style { 18 | fn update(self, el: &mut El) { 19 | let mut new_attrs = Attrs::empty(); 20 | new_attrs.add(At::Class, self); 21 | el.attrs.merge(new_attrs); 22 | } 23 | } 24 | 25 | // #[cfg(target_arch = "wasm32")] 26 | // impl From