0

My vue app is shared between web page and electron app on touch device. On this touch device I want to include extra style. So in my App.vue there is:

<script>
  var is_electron = process.hasOwnProperty("versions") && process.versions.hasOwnProperty("electron")
  var is_embedded = is_electron && require("electron").remote.process.argv.includes("-e")
</script>
<style>
* {
  transition-property: none !important;
  transform: none !important;
  animation: none !important;
  cursor: none !important;
}
</style>

How can I conditionally include this * style ?

1 Answer 1

1

If the platform-specific styling is minimal, I would suggest to use some helper classes. You can even style subcomponents by scoping the css within one of these selectors.

<template>
  <main :class="classes">
    <!-- Hello world -->
  </main>
</template>

<script>
  var isElectron = process.hasOwnProperty("versions") && process.versions.hasOwnProperty("electron")
  var isEmbedded = isElectron && require("electron").remote.process.argv.includes("-e")

  export defualt {
    name: 'App',

    computed: {
      classes () {
        return {
          electron: isElectron,
          embedded: isEmbedded
        }
      }
    }
  }
</script>

<style>
.electron:not(.embedded) * {
  border: 1px solid blue;
}

.embedded:not(.electron) * {
  /* What kind of sorcery is this? */
}

.electron.embedded * {
  border: 1px dotted red;
}

main:not(.electron):not(.embedded) * {
  border: 1px dashed green;
}
</style>
Sign up to request clarification or add additional context in comments.

2 Comments

Why in computed and not in data ?
In this case it would have worked in a data initialisation function. However, I find that in my applications classes change from time to time. The data initialisation function also becomes crowded with different things that are not really related to each other. A computed property at worst adds a function call and a watcher on variables that do not change. In this case I prefer the clearness of a computed property name over adding it to the data attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.