Skip to content

Commit cc58b68

Browse files
mcollovatimshabarov
authored andcommitted
feat: add commercial banner for embedded Flow components (#21960)
1 parent f6b7eac commit cc58b68

File tree

8 files changed

+106
-5
lines changed

8 files changed

+106
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ flow-tests/**/webpack*.js
5151
flow-tests/**/tsconfig.json
5252
flow-tests/**/pnpm-lock.yaml
5353
flow-tests/**/index.html
54+
flow-tests/test-commercial-banner/flow-application/src/main/frontend/**
5455
yarn.lock
5556

5657
flow-client/src/main/frontend/FlowClient.js

flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ private static void modifyIndexHtmlForVite(Document indexHtmlDocument) {
635635
"<script type='text/javascript'>window.JSCompiler_renameProperty = function(a) { return a;}</script>");
636636
}
637637

638-
private static void addCommercialBanner(DeploymentConfiguration config,
638+
static void addCommercialBanner(DeploymentConfiguration config,
639639
Document indexDocument) {
640640
System.clearProperty("vaadin." + Constants.COMMERCIAL_BANNER_TOKEN);
641641
if (config.isProductionMode() && config

flow-server/src/main/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.vaadin.flow.shared.ApplicationConstants;
6666
import com.vaadin.flow.shared.JsonConstants;
6767

68+
import static com.vaadin.flow.server.communication.IndexHtmlRequestHandler.addCommercialBanner;
6869
import static com.vaadin.flow.server.frontend.FrontendUtils.EXPORT_CHUNK;
6970
import static com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8;
7071
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -162,6 +163,8 @@ public Document getBootstrapPage(BootstrapContext context) {
162163

163164
setupCss(head, context);
164165

166+
addCommercialBanner(deploymentConfiguration, document);
167+
165168
return document;
166169
} catch (IOException e) {
167170
throw new BootstrapException(

flow-server/src/main/resources/vite.generated.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,21 @@ export const vaadinConfig: UserConfigFn = (env) => {
741741
if (path !== '/web-component.html') {
742742
return;
743743
}
744-
745-
return [
744+
const scripts = [
746745
{
747746
tag: 'script',
748747
attrs: { type: 'module', src: `/generated/vaadin-web-component.ts` },
749748
injectTo: 'head'
750749
}
751750
];
751+
if (commercialBanner) {
752+
scripts.push({
753+
tag: 'script',
754+
attrs: { type: 'module', src: '/generated/commercial-banner.js' },
755+
injectTo: 'head'
756+
});
757+
}
758+
return scripts;
752759
}
753760
}
754761
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* the License.
1515
*/
1616

17-
package com.vaadin.flow.watermarked.ui;
17+
package com.vaadin.flow.commercialbanner.ui;
1818

1919
import com.vaadin.flow.component.html.Div;
2020
import com.vaadin.flow.component.html.H1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.commercialbanner.ui;
18+
19+
import com.vaadin.flow.component.WebComponentExporter;
20+
import com.vaadin.flow.component.webcomponent.WebComponent;
21+
22+
public class MainViewExporter extends WebComponentExporter<MainView> {
23+
24+
public MainViewExporter() {
25+
super("commercial-embed");
26+
}
27+
28+
@Override
29+
protected void configureInstance(WebComponent<MainView> webComponent,
30+
MainView component) {
31+
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
~ Copyright 2000-2025 Vaadin Ltd.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
~ use this file except in compliance with the License. You may obtain a copy of
6+
~ the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
~ License for the specific language governing permissions and limitations under
14+
~ the License.
15+
-->
16+
17+
<!DOCTYPE html>
18+
<html>
19+
<head>
20+
<meta charset='UTF-8'>
21+
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
22+
<title>Page with Embedded Vaadin Component</title>
23+
24+
<!-- Import the Vaadin web component -->
25+
<script type='module' src='/web-component/commercial-embed.js'></script>
26+
</head>
27+
<body>
28+
<h1>Page with Embedded Vaadin Component</h1>
29+
<!-- Use the component with the tag name -->
30+
<commercial-embed></commercial-embed>
31+
</body>
32+
</html>
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* the License.
1515
*/
1616

17-
package com.vaadin.flow.watermarked.ui;
17+
package com.vaadin.flow.commercialbanner.ui;
1818

1919
import org.junit.Assert;
2020
import org.junit.Test;
@@ -51,4 +51,29 @@ public void shouldAddCommercialBanner() {
5151
"Commercial features require a subscription")));
5252
}
5353

54+
@Test
55+
public void embed_shouldAddCommercialBanner() {
56+
String url = getTestURL() + "/embed.html";
57+
getDriver().get(url);
58+
Assert.assertTrue("Expected embed.html page to be loaded, but was not",
59+
$("h1").withText("Page with Embedded Vaadin Component")
60+
.exists());
61+
WebElement commercialBanner = $("body").single()
62+
// Should unwrap to get shadow root, because TestBenchElement
63+
// throws unsupported operation exception
64+
.getWrappedElement().getShadowRoot()
65+
// By.tagName is not working, using css selector as a workaround
66+
.findElement(By.cssSelector("vaadin-commercial-banner"));
67+
Assert.assertTrue(
68+
"Expected commercial banner component to be shown, but was not",
69+
commercialBanner.isDisplayed());
70+
Assert.assertTrue(
71+
"Expected subscription needed message to be shown, but was not",
72+
commercialBanner.getShadowRoot()
73+
.findElements(By.cssSelector("*")).stream()
74+
.map(WebElement::getText)
75+
.anyMatch(text -> text.contains(
76+
"Commercial features require a subscription")));
77+
}
78+
5479
}

0 commit comments

Comments
 (0)