Skip to content

Commit 2bb591a

Browse files
committed
add an example illustrating how to integrate a dynamic react component
1 parent ceca920 commit 2bb591a

File tree

9 files changed

+95
-0
lines changed

9 files changed

+95
-0
lines changed

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
378378
('title', 'The title of your page. Will be shown in a top bar above the page contents. Also usually displayed by web browsers as the name of the web page''s tab.', 'TEXT', TRUE, TRUE),
379379
('description', 'A description of the page. It can be displayed by search engines when your page appears in their results.', 'TEXT', TRUE, TRUE),
380380
('link', 'The target of the link in the top navigation bar.', 'URL', TRUE, TRUE),
381+
('css', 'The URL of a CSS file to load and apply to the page.', 'URL', TRUE, TRUE),
382+
('javascript', 'The URL of a Javascript file to load and execute on the page.', 'URL', TRUE, TRUE),
381383
('image', 'The URL of an image to display next to the page title.', 'URL', TRUE, TRUE),
382384
('icon', 'Name of an icon (from tabler-icons.io) to display next to the title in the navigation bar.', 'TEXT', TRUE, TRUE),
383385
('menu_item', 'Adds a menu item in the navigation bar at the top of the page. The menu item will have the specified name, and will link to as .sql file of the same name.', 'TEXT', TRUE, TRUE),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Custom javascript and CSS
2+
3+
This example shows how to use custom javascript and CSS in your application.
4+
5+
It integrates a simple [react](https://reactjs.org/) component and loads it with properties coming from a SQL query.
6+
7+
## Screenshots
8+
9+
![example SQLPage application with a custom style](screenshot-css.png)
10+
11+
![example client-side reactive SQLPage application with React](screenshot-react.png)
12+
13+
## Notes
14+
15+
This example relies on a CDN to load the react library, and the example component is written in plain Javscript, not JSX.
16+
17+
You can also use include a local copy of react, and write your components in JSX/TSX,
18+
you will simply need to add a build step to your application to compile the JSX/TSX files into plain JS,
19+
and then include that build result in your application.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT 'shell' AS component,
2+
'SQLPage with a frontend component' as title,
3+
'style.css' as css,
4+
'settings' as icon;
5+
6+
SELECT 'text' AS component, 'funky_text' AS id;
7+
SELECT 'Try my react component !' AS contents, 'react.sql' AS link;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Here we are using React and ReactDOM directly, but this file could be a compiled
2+
// version of a React component written in JSX.
3+
4+
function RootComponent({ greeting_name }) {
5+
const [count, setCount] = React.useState(0);
6+
return React.createElement(
7+
'button',
8+
{ onClick: () => setCount(count + 1), className: 'btn btn-primary' },
9+
`Hello ${greeting_name}, you clicked me ${count} times!`
10+
);
11+
}
12+
13+
for (const container of document.getElementsByClassName('react_component')) {
14+
const root = ReactDOM.createRoot(container);
15+
const props = JSON.parse(container.dataset.props);
16+
root.render(React.createElement(RootComponent, props, null));
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT 'shell' AS component,
2+
'SQLPage with a frontend component' AS title,
3+
'settings' AS icon,
4+
5+
-- Including react from a CDN like that is quick and easy, but if your project grows larger,
6+
-- you might want to use a bundler like webpack, and include your javascript file here instead
7+
'https://cdn.jsdelivr.net/npm/[email protected]/umd/react.production.min.js' AS javascript,
8+
'https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.production.min.js' AS javascript,
9+
'my_react_component.js' AS javascript;
10+
11+
SELECT 'react_component' AS component,
12+
JSON_OBJECT(
13+
'greeting_name', 'World'
14+
) AS props;
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="react_component" data-props="{{props}}">
2+
Dynamic component is loading...
3+
</div>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#funky_text {
2+
font-family: Arial, sans-serif;
3+
font-size: 36px;
4+
color: white;
5+
background: linear-gradient(45deg, #ff4e00, #ec9f05);
6+
padding: 10px;
7+
border-radius: 10px;
8+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
9+
transition: transform 0.3s ease-in-out;
10+
}
11+
12+
#funky_text:hover {
13+
transform: scale(1.1);
14+
background: linear-gradient(45deg, #e5004d, #7a0180);
15+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
16+
}
17+
18+
@keyframes neon-glow {
19+
0%, 100% {
20+
text-shadow: 0 0 10px #fa2dd1, 0 0 20px #b30890, 2px 3px 30px #ff00cc;
21+
}
22+
50% {
23+
text-shadow: 0 0 1px #e48fd3, 0 0 2px #ca28aa, 0 0 8px #ff00cc;
24+
}
25+
}
26+
27+
#funky_text:hover {
28+
animation: neon-glow .5s ease-in-out infinite;
29+
}
30+
31+
#funky_text * {
32+
color: inherit;
33+
}

0 commit comments

Comments
 (0)