How to use the @vue/composition-api.computed function in @vue/composition-api

To help you get started, we’ve selected a few @vue/composition-api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github posva / pinia / src / store.ts View on Github external
const storeWithState: Store = {
    id,
    // it is replaced below by a getter
    state: state.value,

    patch,
    subscribe,
    reset,
  }

  // @ts-ignore we have to build it
  const computedGetters: StoreGetters = {}
  for (const getterName in getters) {
    const method = getters[getterName]
    // @ts-ignore
    computedGetters[getterName] = computed>(() =>
      getters[getterName](state.value)
    )
  }

  const store = {
    ...storeWithState,
    ...computedGetters,
  }

  // make state access invisible
  Object.defineProperty(store, 'state', {
    get: () => state.value,
    set: (newState: S) => {
      isListening = false
      state.value = newState
      isListening = true
github dawg / vusic / src / components / Slider.vue View on Github external
return `${p1.x},${p1.y} ${p2.x},${p2.y} ${p3.x},${p3.y}`;
    });

    const scaled = computed(() => {
      return scale(props.value, [props.min, props.max], [0, 1]);
    });

    const position = computed(() => {
      return props.height - (props.height * scaled.value);
    });

    const rightHeight = computed(() => {
      return props.right * props.height;
    });

    const leftHeight = computed(() => {
      return props.left * props.height;
    });

    const svg = ref(null);
    function move(e: MouseEvent) {
      if (!svg.value) {
        return;
      }
      let volume = svg.value.getBoundingClientRect().top + props.height - e.clientY;
      volume /= props.height;
      volume = Math.max(Math.min(volume, 1), 0);
      volume = scale(volume, [0, 1], [props.min, props.max]);
      context.emit('input', volume);
      update();
    }
github dawg / vusic / src / dawg / extensions / extra / project-name.ts View on Github external
activate() {
    const openedFile = dawg.project.openedFile;
    const projectName = computed(() => {
      return openedFile.value === null ? '' : path.basename(openedFile.value).split('.')[0];
    });

    const component = Vue.extend({
      template: `
        <div class="text-default text-sm">
          {{ projectName.value }}
        </div>
      `,
      data: () =&gt; ({
        projectName,
      }),
    });

    dawg.ui.statusBar.push({
      component,
github dawg / vusic / src / components / TooltipIcon.vue View on Github external
setup(props, context) {
    const icon = computed(() => {
      const slot = context.slots.default();
      if (!slot || !slot[0]) {
        return '';
      }

      const text = slot[0].text || '';
      return text.trim();
    });

    const showTooltip = computed(() => {
      return !!props.tooltip;
    });

    const style = computed(() => {
      return { color: props.color };
    });

    return {
      style,
      showTooltip,
      icon,
    };
  },
});
github metaspace2020 / metaspace / metaspace / webapp / src / components / IonImageViewer.tsx View on Github external
const usePixelIntensityDisplay = (props: Props, imageLoaderRef: Ref) =&gt; {
  const pixelIntensityTooltipRef = templateRef('pixelIntensityTooltip')
  const cursorPixelPos = ref&lt;[number, number] | null&gt;(null)
  const zoomX = computed(() =&gt; props.zoom)
  const zoomY = computed(() =&gt; props.zoom / props.pixelAspectRatio)
  const cursorOverPixelIntensity = computed(() =&gt; {
    if (props.ionImage != null &amp;&amp; cursorPixelPos.value != null) {
      const [x, y] = cursorPixelPos.value
      const { width, height, mask, intensityValues } = props.ionImage
      if (x &gt;= 0 &amp;&amp; x &lt; width
        &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; height
        &amp;&amp; mask[y * width + x] !== 0) {
        return intensityValues[y * width + x]
      }
    }
    return null
  })
  const pixelIntensityStyle = computed(() =&gt; {
    if (props.showPixelIntensity
      &amp;&amp; props.ionImage != null
      &amp;&amp; cursorPixelPos.value != null
      &amp;&amp; cursorOverPixelIntensity.value != null) {
github dawg / vusic / src / components / Waveform.vue View on Github external
return '0 0 ' + steps.value + ' ' + props.height;
    });

    const h2 = computed(() => {
      return props.height / 2;
    });

    const steps = computed(() => {
      return props.width * 100;
    });

    const data0 = computed(() => {
      return props.buffer.getChannelData(0);
    });

    const data1 = computed(() => {
      return props.buffer.numberOfChannels > 1 ? props.buffer.getChannelData(1) : data0.value;
    });

    const dataLen = computed(() => {
      return data0.value.length;
    });

    const duration = computed(() => {
      return props.buffer.duration - props.offset;
    });

    const step = computed(() => {
      return duration.value / props.buffer.duration * dataLen.value / steps.value;
    });

    const ind = computed(() => {
github pikax / vue-composable / packages / core / src / pagination / arrayPagination.ts View on Github external
export function useArrayPagination, TR&gt;(
  array: RefTyped,
  options?: Partial&gt;
): ArrayPaginationResult {
  const arrayRef = wrap(array);

  const pagination = usePagination({
    ...{
      currentPage: 1,
      pageSize: 10,
    },
    ...options,
    total: computed(() =&gt; arrayRef.value.length)
  });

  const result = computed(() =&gt; {
    const array = arrayRef.value;
    if (!Array.isArray(array)) return [];
    return array.slice(
      pagination.offset.value,
      pagination.offset.value + pagination.pageSize.value
    );
  }) as Readonly&gt;;

  return {
    ...pagination,
    result
  };
}
github logaretm / vue-use-web / src / PreferredColorScheme.ts View on Github external
export function usePreferredColorScheme(): Ref&lt;'dark' | 'light' | 'no-preference'&gt; {
  const queries = {
    light: '(prefers-color-scheme: light)',
    dark: '(prefers-color-scheme: dark)',
    'no-preference': '(prefers-color-scheme: no-preference)'
  };

  const isDark = useMedia(queries.dark);
  const isLight = useMedia(queries.light);

  const theme = computed(() =&gt; {
    if (isDark.value) {
      return 'dark';
    }

    if (isLight.value) {
      return 'light';
    }

    return 'no-preference';
  });

  return theme;
}
github nuxt / typescript / docs / cookbook / components / script.composition-api.ts View on Github external
setup ({ user }) {
    const fullName = computed(() => `${user.firstName} ${user.lastName}`)
    const message = reactive('This is a message')

    return {
      fullName,
      message
    }
  }
})
github pikax / vue-composable / dist / vue-composable.es.js View on Github external
if (isJson) {
            const pJson = response
                .json()
                .then(x => (json.value = x))
                .catch(x => {
                json.value = null;
                jsonError.value = x;
            });
            if (parseImmediate) {
                await pJson;
            }
        }
        return response;
    });
    const status = computed(() => (use.result.value && use.result.value.status) || null);
    const statusText = computed(() => (use.result.value && use.result.value.statusText) || null);
    return {
        ...use,
        json,
        jsonError,
        status,
        statusText
    };
}