Skip to content

Commit

Permalink
feat(Cursor): Add Cursor component to drive cursor style
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jan 11, 2023
1 parent 344c5ca commit 6093f51
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions trame_components/widgets/trame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = [
"ClientStateChange",
"ClientTriggers",
"Cursor",
"LifeCycleMonitor",
"MouseTrap",
"SizeObserver",
Expand Down Expand Up @@ -75,6 +76,21 @@ def call(self, method, *args):
self.server.js_call(self.__name, "emit", method, *args)


# -----------------------------------------------------------------------------
# TrameClientTriggers
# -----------------------------------------------------------------------------
class Cursor(HtmlElement):
"""Allow to define the cursor of the parent component based on an active index and the list of css cursor names.
:param active: index to selection the current cursor
:param cursors: Array of available cursor to chose from
"""

def __init__(self, **kwargs):
super().__init__("trame-cursor", **kwargs)
self._attr_names += ["active", "cursors"]


# -----------------------------------------------------------------------------
# TrameLifeCycleMonitor
# -----------------------------------------------------------------------------
Expand Down
38 changes: 38 additions & 0 deletions vue-components/src/components/Cursor.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script>
export default {
name: 'TrameCursor',
props: {
active: {
type: Number,
default: 0,
},
cursors: {
type: Array,
default: () => ['default', 'wait'],
},
},
computed: {
activeCursor() {
return this.cursors[this.active] || 'default';
},
},
watch: {
activeCursor(name) {
this.updateCursor(name);
},
},
methods: {
updateCursor(name) {
if (this?.$el?.parentElement) {
this.$el.parentElement.style.cursor = name;
}
},
},
mounted() {
this.updateCursor(this.activeCursor);
},
beforeDestroy() {
this.updateCursor('default');
},
};
</script>
2 changes: 2 additions & 0 deletions vue-components/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TrameClientStateChange from './ClientStateChange';
import TrameClientTriggers from './ClientTriggers';
import TrameCursor from './Cursor';
import TrameFloatCard from './FloatCard';
import TrameGitTree from './GitTree';
import TrameLifeCycleMonitor from './LifeCycleMonitor';
Expand All @@ -12,6 +13,7 @@ import TrameXaiImage from './XaiImage';
export default {
TrameClientStateChange,
TrameClientTriggers,
TrameCursor,
TrameFloatCard,
TrameGitTree,
TrameLifeCycleMonitor,
Expand Down

0 comments on commit 6093f51

Please sign in to comment.